[英]previous declaration of- was Here and “conflicting types for put_non_bloccante”
對不起,我的文件buffer.c中有一個錯誤,提示“類型沖突”,而我的文件buffer.h中有錯誤,提示“ put_non_bloccante的先前聲明在這里!
/*
* Buffer.c
*
* Created on: 19/nov/2013
* Author: lele
*/
#include "buffer.h"
#include <stdio.h>
#include "msg.h"
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_ERROR (msg_t *) NULL
/* allocazione / deallocazione buffer */
// creazione di un buffer vuoto di dim. max nota
buffer_t * buffer_init(unsigned int maxsize){
buffer_t * new_buffer = (buffer_t*)malloc( sizeof(buffer_t) );
new_buffer->T=0;
new_buffer->D=0;
new_buffer->msg_presenti=0;
new_buffer->array_msg =(msg_t **)malloc(sizeof(msg_t*) * maxsize);
new_buffer->size=maxsize;
pthread_cond_init(&(new_buffer->not_empty),NULL);
pthread_cond_init(&(new_buffer->not_full),NULL);
pthread_mutex_init(&(new_buffer->mutex),NULL);
return new_buffer;
}
// deallocazione di un buffer
void buffer_destroy(buffer_t* buffer){
int i;
for(i=0;i<buffer->size;i++){
msg_destroy_string(buffer->array_msg[i]);
}
free(buffer->array_msg);
free(buffer);
}
msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg){
// inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
// altrimenti effettua l'inserimento e restituisce il messaggio
// inserito; N.B.: msg!=null
if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){
return BUFFER_ERROR;
}
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));
}
buffer->array_msg[buffer->D]=msg;
buffer->D = (buffer->D+1)%buffer->size;
buffer->msg_presenti= buffer->msg_presenti+1;
pthread_cond_signal(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
// estrazione bloccante: sospende se vuoto, quindi
// restituisce il valore estratto non appena disponibile
msg_t* get_bloccante(buffer_t* buffer){
// estrazione non bloccante: restituisce BUFFER_ERROR se vuoto
// ed il valore estratto in caso contrario
pthread_mutex_lock(&(buffer->mutex));
while(buffer->msg_presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t * msg =buffer->array_msg[buffer->T];
buffer->T=(buffer->T+1)%buffer->size;
buffer->msg_presenti--;
pthread_cond_signal(&(buffer->not_full));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
msg_t* get_non_bloccante(buffer_t* buffer){
if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){
return BUFFER_ERROR;
}
while(buffer->msg_presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t* msg =buffer->array_msg[buffer->T];
buffer->T=(buffer->T+1)%buffer->size;
buffer->msg_presenti--;
pthread_cond_signal(&(buffer->not_full));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
msg_t* put_bloccante(buffer_t *buffer, msg_t *msg){
// inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
// altrimenti effettua l'inserimento e restituisce il messaggio
// inserito; N.B.: msg!=null
pthread_mutex_lock(&(buffer->mutex));
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));
}
buffer->array_msg[buffer->D]=msg;
buffer->D = (buffer->D+1)%buffer->size;
buffer->msg_presenti= buffer->msg_presenti+1;
pthread_cond_signal(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
這是我的buffer.h文件! 我該怎么辦 ?:
#include <pthread.h>
#include "msg.h"
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer {
int size;
int T;
int D;
int msg_presenti;
pthread_cond_t not_full;
pthread_cond_t not_empty;
pthread_mutex_t mutex;
msg_t ** array_msg;
} buffer_t;
buffer_t * buffer_init(unsigned int maxsize);
void buffer_destroy(buffer_t* buffer);
msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg);
buffer.c包含“ msg.h”,buffer.h也包含。
解決此問題的最簡單方法是將其放在msg.h的頂部
#ifndef MSG_H_
#define MSG_H_
而這在最底層
#endif
您應該對buffer.h做類似的事情。 這避免了單個翻譯單元中包含多個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.