在我的一个文件中(我被告知我不允许修改)我不断收到以下错误:
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
:只需要包含#include部分以供参考
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "st.h"
#include "buffer.h"
答案 0 :(得分:0)
如果在semaphore.h中添加#include "st.h"
可以解决问题,你可以在buffer.h中切换包含序列:
#include "st.h"
#include "semaphore.h"
或者,如果您不允许这样做,请在buffer.c中的buffer.c之前#include "st.h"