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:只是要包括#include部分以供参考

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

1 回答 1

0

如果添加#include "st.h"semaphore.h 可以解决问题,您也可以切换 buffer.h 中包含的顺序:

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

或者,如果您也不允许这样做,则#include "st.h"在 buffer.c 中的 buffer.h 之前

于 2013-10-09T13:57:29.113 回答