2013-10-09 3 views
0

В одном из моих файлов (которые я сказали, что я не могу изменять) я получаю следующее сообщение об ошибке: ошибка: ожидается спецификатор-классификатор-лист перед тем

In file included from buffer.h:1, 
      from buffer.c:4: 
semaphore.h:4: error: expected specifier-qualifier-list before ‘st_cond_t’ 

Когда я добавляю библиотека st.h, она компилируется и работает нормально, но мне сказали, что мне не разрешено изменять этот файл, поэтому мне нужно найти способ обойти его, и я не уверен, как это сделать. Какие-либо предложения? Я разместим соответствующий код ниже.

semaphore.h: не разрешено редактировать этот файл

typedef struct 
{ 
    int value; 
    st_cond_t sem_queue; 
} semaphore; 

void down(semaphore *s); 
void up(semaphore *s); 
void createSem(semaphore *s, int value); 

buffer.h

#include "semaphore.h" 
#include "st.h" 
#pragma once //necessary to avoid compiler errors 

/*this file just defines the structure of a buffer and the methods that will be associated with it. Not too complex */ 

typedef struct 
{ 
    semaphore *emptyBuffer; //need two semaphores according to slides 
    semaphore *fullBuffer; 
    int nextIn; 
    int nextOut; 
    int size; 
    char *chars; //literall buffer object 
}buffer; 

void c_deposit(buffer *buffer, int c); //declaring our functions, similar to an interface in java 
int c_remove(buffer *buffer); 

buffer *init_buffer(int size); 

buffer.c

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "buffer.h" 
#include "st.h" 

/* This file fleshes out the methods declared in the header. c_deposit/c_remove are identical in logic to the slides for this assignment, they follow the basic consumer/producer dynamic. init_buffer initializes and provides memory for the elements associated with a buffer struct so that we can access them later */ 

//prototypes 
void c_deposit(buffer *buffer, int c); 
int c_remove(buffer *buffer); 

buffer *init_buffer(int size); 

void c_deposit(buffer *buffer, int c) //code pretty much taken from slides 
{ 
    down(buffer->emptyBuffer); //called down semaphore 
    buffer->chars[buffer->nextIn]=c; //put char on char buffer 
    buffer->nextIn=(buffer->nextIn+1)%buffer->size; //increment counter 
    up(buffer->fullBuffer); //call up semaphore 
} 
int c_remove(buffer *buffer) //almost identical to deposit 
{ 
    int c; 
    down(buffer->fullBuffer); 
    c=buffer->chars[buffer->nextOut]; //pull off char 
    buffer->nextOut=(buffer->nextOut+1)%buffer->size; //increment 
    up(buffer->emptyBuffer); 
    return c; //return char 
} 
buffer *init_buffer(int size) //initializing components of our buffer 
{ 
    //want to malloc so that we don't lose it when we return 
    buffer *new_Buffer; 
    new_Buffer=malloc((sizeof(buffer))); 

    semaphore *sem; //malloc a semaphore and then set the buffer empty/full semaphores equal to it so don't lose it 
    sem=malloc(sizeof(semaphore)); 

    new_Buffer->emptyBuffer=sem; //same as last comment 
    createSem(new_Buffer->emptyBuffer, size); //have to create the semaphore 

    semaphore *sem2; 
    sem2=malloc(sizeof(semaphore)); 

    new_Buffer->fullBuffer=sem2; 
    createSem(new_Buffer->fullBuffer, 0); 

    char *array; //malloc for the char buffer 
    array=malloc(sizeof(char)*size); 
    new_Buffer->chars=array; 

    new_Buffer->size=size; 

    int nextIn=0; //initialize the basic ints 
    new_Buffer->nextIn=nextIn; 

    int nextOut=0; 
    new_Buffer->nextOut=nextOut; 

    return new_Buffer; 
} 

main.c: просто идти ING включить секцию #include для справки

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include "st.h" 
#include "buffer.h" 
+0

показать нам 'semaphore.h', потому что там, где проблема –

+0

следует добавить сейчас – sreya

ответ

1

При добавлении #include "st.h" в semaphore.h бы решить эту проблему, можно альтернативно переключить последовательность включает в buffer.h:

#include "st.h" 
#include "semaphore.h" 

или, если вам не разрешено это делать, #include "st.h" до buffer.h в буфере.c

+0

Вау, что сработало, я не знал, что порядок имеет значение! Почему это? – sreya

+0

Это потому, что '# include' является просто заменой текста, и если вы используете какой-то тип типа' st_cond_t' где-то в вашем коде, вам нужно 'typedef' его ** перед ** этой строкой –

Смежные вопросы