【发布时间】:2021-01-07 07:29:57
【问题描述】:
我是一名来自 python 的开发人员。我有一个 C 头文件,但是当我尝试使用它时,我得到了很多语法错误。
我正在使用 MSVC 编译器。
代码如下:
stack.h
typedef struct stack
{
void* data;
Stack* next;
} Stack;
Stack* _stack_init(Stack* s, void* item);
void _stack_push(Stack* s, void* item);
void _stack_pop(Stack* s);
stack.c:
#include <stdlib.h>
typedef struct stack
{
void* data;
struct stack* next;
} Stack;
Stack* _stack_init(Stack* s, void* item) {
Stack* new_stack = (Stack*) malloc((sizeof(Stack)));
new_stack->data = &item;
}
void _stack_push(Stack* s, void* item) {
if (s->next != NULL) {
_stack_push(s->next, item);
}
Stack* new_item = _stack_init(s, item);
s->next = new_item;
}
void _stack_pop(Stack* s) {
if (s->next == NULL) return s;
else _stack_pop(s->next);
}
这是错误:
g:\progs\c\csv_reader\libs/structures/stack.h(4): error C2061: syntax error: identifier 'Stack'
g:\progs\c\csv_reader\libs/structures/stack.h(5): error C2059: syntax error: '}'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2059: syntax error: 'type'
g:\progs\c\csv_reader\libs/structures/stack.h(7): error C2059: syntax error: ')'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2059: syntax error: 'type'
g:\progs\c\csv_reader\libs/structures/stack.h(8): error C2059: syntax error: ')'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2143: syntax error: missing ')' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2143: syntax error: missing '{' before '*'
g:\progs\c\csv_reader\libs/structures/stack.h(9): error C2059: syntax error: ')'
我个人看不到任何语法错误。谁能向我解释这个错误? 提前感谢您的帮助!
【问题讨论】:
-
有很多问题...您甚至没有在您的
stack.c文件中包含stack.h。当您遇到一堆错误时,只需修复第一个错误:在您的情况下,您使用的类型 (Stack) 尚不为人所知。使用前向声明。 -
天啊!我是新手。也许我应该先回去掌握基础知识。与 python 相比,C 有很多陷阱????
标签: c syntax syntax-error