【发布时间】:2016-03-25 12:01:23
【问题描述】:
我有以下代码:
#include "stdio.h"
typedef struct StackEntry StackEntry;
typedef struct StackEntry
{
int data;
StackEntry *next;
};
StackEntry* createStack()
{
return NULL;
}
int main()
{
StackEntry *stack = createStack(); //Error: incompatible types when initializing type 'struct StackEntry *' using type 'StackEntry'
}
如上所示,我收到错误incompatible types when initializing type 'struct StackEntry *' using type 'StackEntry'。很长一段时间后我正在重新访问C。我在这里做错了什么?
编辑(非常抱歉)
我不必要地简化了一些东西。我觉得这将有助于突出显示错误代码。我原来的设置有很多代码。它也在工作。但现在它坏了。所以我收集了所有相关的代码并放在这里。我应该首先尝试将它们全部放在一个文件中并运行。我很抱歉没有这样做。但现在我已经剥离了所有可能的代码并能够重现错误。这里是:
算法.h
#ifndef ALGORITHMS_H_
#define ALGORITHMS_H_
StackEntry createStack();
#endif /* ALGORITHMS_H_ */
数据结构.h
#ifndef DATASTRUCTURES_H_
#define DATASTRUCTURES_H_
typedef struct StackEntry StackEntry;
struct StackEntry
{
int data;
StackEntry *next;
};
#endif /* DATASTRUCTURES_H_ */
stack.c
#include "datastructures.h"
#include "stdio.h"
StackEntry* createStack()
{
return NULL;
}
Main.c
#include "datastructures.h"
#include "algorithms.h"
int main()
{
StackEntry *stack = createStack(); //Error: incompatible types when initializing type 'struct StackEntry *' using type 'StackEntry'
return 0;
}
【问题讨论】:
-
为什么会有第二个 typedef,因为它会被忽略?
-
去掉没用的第二个
typedef后还有错误吗? -
啊我错过了...它不应该存在...顺便说一句为什么编译器不认为它是错误...?
-
这是 C 还是 C++?它们是两种不同的语言,处理类名的方式不同
-
哦,这是在
.c文件中