枚举bool的冲突类型? [英] Conflicting types for enum bool?
问题描述
我有一个程序给我错误 [错误]冲突的类型为'空'
和 [错误]冲突类型为'全'
。我有一个预感,它与枚举bool
使用有关(这是我第一次尝试使用它)。我已经看过其他类似的问题,这并不能帮助我,问题是忘记在程序中声明一个原型。我已经确定在main之前写我的函数。
这是我的代码:
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
typedef struct {
char ** data; //表示堆栈的字符串数组
int top; // -1 if empty
int size;
}堆栈;
typedef枚举{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- >顶]);
ps-> top--;
}
免费(ps->数据);
}
void push(Stack * ps,char * str,int * size){//可能需要调用realloc bfr complete push
if(full(ps)) {
char ** temp = realloc(ps-> data,ps-> size * sizeof(char *));
ps-> data == temp;
printf(堆栈容量从%d增长到%d元素\,ps-> size ** size,ps-> size **(++ size));
}
ps-> data [++ ps-> top] = str;
}
char * pop(Stack * s,int * i){//返回从堆栈中删除的字符串
if(empty(s))
返回NULL;
printf(弹出后的元素数:%d\tstring popped:%s\\\
, - i,s-> data [s-> top]);
return s-> data [s-> top--];
}
bool empty(Stack s){//如果stack没有元素else则返回true false
if(s.top == -1)
return真正;
return FALSE;
}
bool full(Stack s){//如果没有更多的房间,则返回true false
if(s.top == s.size-1)
返回TRUE;
return FALSE;
}
int main(int argc,char * argv []){
printf(Assignment 2 Problem 1 by Jasmine Ramirez\\\
);
FILE * input = fopen(data_a2.txt,r);
if(input == NULL){
printf(File%s not found.\\\
,data_a2.txt);
return -1;
}
Stack * s;
s = create();
char str [255];
int i = 0,size = 1;
while(fscanf(input,%[^ \\\
],str)== 1){
i ++;
if(strcmp(str,pop)== 0){
i--;
pop(s,& i);
// printf(#of element after popping:%d\tstring popped:%s,i,temp);
}
else {
push(s,str,& size);
}
}
deleteStack(s);
fclose(input);
return 0;
}
这是输入: (以防万一)
pre>
确定
Be
pop
pop
pop
你
停止
不会
感觉
a
有
I
但
$ $ $ $ $ $ $ $ $ $ $
pop
something
和
都
他们
戒指
故事
ovaltine
你的
pop
pop
圣诞节
A
-
pop
pop
pop
pop
想法?或者我刚刚完全关闭?
解决方案在函数
push()
你有:void push(Stack * ps,char * str,int * size){
if (ps)){
...
这隐式声明
full
作为具有返回int
的不确定参数列表的函数。您稍后将其定义为返回bool
- 这些是不同的类型,因此您会收到错误。
声明或在使用之前定义
full
。类似的评论适用于empty
,但是由弗拉德从莫斯科在他的答案。
如果您使用GCC,请使用诸如
-Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration
(或与您的版本支持的许多声明一样),以确保不再遇到这种情况。I have a program that gives me the error
[Error] conflicting types for 'empty'
and[Error] conflicting types for 'full'
. I have a hunch that it has something to do with theenum bool
use (this is the first time I have tried using it). I've looked at other similar questions, that do not help me, were the issue is forgetting to declare a prototype in the program. I have made sure to write my functions before of main.This is my code:
#include <stdio.h> #include <stdlib.h> #include <string.h> 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; }
This is the input: (just in case)
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
Ideas? Or am I just completely off?
解决方案In function
push()
you have:void push(Stack* ps, char* str, int* size){ if(full(ps)){ …
This implicitly declares
full
as a function with indeterminate argument list returning anint
. You later define it as returning abool
— these are different types, and hence you get the error.Declare or define
full
before you use it. Similar comments apply toempty
, but there's an additional problem in that code pointed out by Vlad from Moscow in his answer.If you use GCC, use options such as
-Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration
(or as many of them as your version supports) to ensure that you don't run into this again.这篇关于枚举bool的冲突类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!