gpt4 book ai didi

c - 错误 : expected specifier-qualifier-list before

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:21 25 4
gpt4 key购买 nike

我正在尝试编写代码来构建堆栈,但遇到了对我来说没有意义的编译错误。这是我的 stack.h:

struct StackNode {

void* previous;
int value;
};


struct Stack {

StackNode* top;
};


Stack* new_stack () {

StackNode stn = { NULL, 0 };
Stack* st = (Stack*) malloc(sizeof(Stack));
st->top = NULL;
return st;
}

和我的main.c:

#include <stdio.h>
#include <stdlib.h>

#include "stack.h"

int main () {

struct Stack* st = new_stack();

return 0;

}

gcc 抛出这些错误:

make (in directory: /home/diego/temp/stack) gcc -g -O2 -std=c99 -c

main.c In file included from main.c:4: Compilation failed. stack.h:10:

error: expected specifier-qualifier-list before ‘StackNode’

stack.h:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute

before ‘*’ token main.c: In function ‘main’: main.c:8: warning:

implicit declaration of function ‘new_stack’ main.c:8: warning:

initialization makes pointer from integer without a cast make: *

[main.o] Error 1

编辑:我发现了错误。我忘记在某些行中将 struct 放在 StackStackNode 之前。始终在这些行上使用 struct 可以解决问题。

最佳答案

改变:

struct Stack {
StackNode* top;
};

到:

struct Stack {
struct StackNode* top;
};

和任何其他使用 StackNodeStack 且前面没有 struct 的地方。如果您不想指定 struct,您可以使用 typedef

关于c - 错误 : expected specifier-qualifier-list before,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12154850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com