enum bool的冲突类型?

我有一个程序,它给出[Error] conflicting types for 'full' [Error] conflicting types for 'empty'[Error] conflicting types for 'full' 。 我有一种预感,它与enum bool使用有关(这是我第一次尝试使用它)。 我看过其他类似的问题,这对我没有帮助,问题是忘记在程序中声明原型。 我确保在main之前编写我的函数。

这是我的代码:

 #include  #include  #include  typedef struct{ char** data; // array of strings represnting the stack int top; // -1 if empty int size; }Stack; typedef enum { FALSE, TRUE } bool; Stack* create(){ Stack *s; s = (Stack*)malloc(sizeof(Stack)); s->top = -1; s->size = 10; s->data = (char**)malloc(s->size*sizeof(char*)); return s; } void deleteStack(Stack* ps){ while(ps->top = 0){ free(ps->data[ps->top]); ps->top--; } free(ps->data); } void push(Stack* ps, char* str, int* size){ //may need to call realloc bfr completing push if(full(ps)){ char **temp = realloc(ps->data, ps->size*sizeof(char*)); ps->data == temp; printf("Stack capacity has grown from %d to %d elements\n", ps->size**size, ps->size**(++size)); } ps->data[++ps->top] = str; } char* pop(Stack* s, int* i){ //returns the string that was removed from the stack if(empty(s)) return NULL; printf("#of elements after popping: %d\tstring popped: %s\n", --i, s->data[s->top]); return s->data[s->top--]; } bool empty(Stack s){ // returns true if stack has no elements else false if(s.top == -1) return TRUE; return FALSE; } bool full(Stack s){ //returns true if no more room else false if(s.top == s.size-1) return TRUE; return FALSE; } int main(int argc, char *argv[]){ printf("Assignment 2 Problem 1 by Jasmine Ramirez\n"); FILE * input = fopen("data_a2.txt", "r"); if(input == NULL){ printf("File %s not found.\n", "data_a2.txt"); return -1; } Stack *s; s = create(); char str[255]; int i = 0, size = 1; while(fscanf(input, "%[^\n]", str) == 1){ i++; if(strcmp(str, "pop") == 0){ i--; pop(s, &i); //printf("#of elements after popping: %d\tstring popped: %s", i, temp); } else{ push(s, str, &size); } } deleteStack(s); fclose(input); return 0; } 

这是输入:(以防万一)

 to sure Be pop pop pop you stop won't that feeling a have I but on go to much Not right Brilliant happens drink pop something and both them Ring Story ovaltine your pop pop Christmas A -- pop pop pop pop 

想法? 还是我完全脱了?

在函数push()你有:

 void push(Stack* ps, char* str, int* size){ if(full(ps)){ … 

这隐式声明为一个函数,其中不确定的参数列表返回一个int 。 您稍后将其定义为返回bool – 这些是不同的类型,因此您会收到错误。

在使用之前声明或定义full 。 类似的评论适用于empty ,但弗拉德在他的回答中指出的代码还有一个问题。

如果您使用GCC,请使用-Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration (或者您的版本支持的许多选项)以确保您不要再碰到这个了。

在声明之前使用名称为emptyfull的名称。 例如

 char* pop(Stack* s, int* i){ //returns the string that was removed from the stack if(empty(s)) ^^^^^^^^ return NULL; printf("#of elements after popping: %d\tstring popped: %s\n", --i, s->data[s->top]); return s->data[s->top--]; } bool empty(Stack s){ // returns true if stack has no elements else false if(s.top == -1) return TRUE; return FALSE; } 

您必须在使用之前声明它们。

而且例如函数empty具有Stack类型的参数,同时使用Stack *类型的参数调用