我有一个程序可以提供错误[Error] conflicting types for 'empty'
和[Error] conflicting types for 'full'
。我有一种预感,它与enum bool
使用有关(这是我第一次尝试使用它)。我已经看过其他类似的问题,这对我没有帮助,问题是忘记在程序中声明原型。我确保在main之前编写我的函数。
这是我的代码:
#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;
}
这是输入:(以防万一)
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
想法?或者我完全离开了?
答案 0 :(得分:2)
在功能push()
中,你有:
void push(Stack* ps, char* str, int* size){
if(full(ps)){
…
这隐含地将full
声明为具有返回int
的不确定参数列表的函数。您稍后将其定义为返回bool
- 这些是不同的类型,因此您会收到错误。
在使用之前声明或定义full
。类似的评论适用于empty
,但Vlad from Moscow在其answer中指出的代码中存在其他问题。
如果您使用GCC,请使用-Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration
等选项(或许多版本支持的选项),以确保您不再遇到此问题。
答案 1 :(得分:1)
在声明之前使用名称<script src="https://maps.googleapis.com/maps/api/jskey=AIzaSyC4NNeHbNb1_nFhKnckNxMKpxXeQUecenM&libraries=places" async defer></script>
和empty
。例如
full
您必须在使用前声明它们。
此外,例如,函数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
类型的参数调用