C 不理解我';我越来越
因此,我有以下.h文件:StudentRosterDef.h和StudentRosterDef.h StudentRosterDef.h:C 不理解我';我越来越,c,pointers,struct,forward-declaration,C,Pointers,Struct,Forward Declaration,因此,我有以下.h文件:StudentRosterDef.h和StudentRosterDef.h StudentRosterDef.h: typedef struct Node *NodeP; typedef struct Student *StudentP; 学生名册 typedef struct StudentRoster *StudentRosterP; //Below prototype creates empty studentroster and returns a NULL
typedef struct Node *NodeP;
typedef struct Student *StudentP;
学生名册
typedef struct StudentRoster *StudentRosterP;
//Below prototype creates empty studentroster and returns a NULL pointer
StudentRosterP newStudentRoster();
现在,我有下面的.c文件要随附:studentrolister.c
学生名册c:
#include "StudentRosterDef.h"
#include "StudentRoster.h"
struct StudentRosterP
{
NodeP root;
};
struct NodeP
{
StudentP left;
StudentP right;
};
StudentRosterP newStudentRoster()
{
StudentRosterP thisRoster = (StudentRosterP) malloc(sizeof(StudentRosterP));
thisRoster->root = 0x00;
thisRoster = 0x00;
return thisRoster;
};
以下是在终端上运行gcc命令后得到的消息:
StudentRoster.c:27:12 : error: incomplete definition type of 'struct StudentRoster'
thisRoster->root = 0x00;
~~~~~~~~^
./StudentRoster.h:14:16: note: forward declaration of 'struct StudentRoster'
typedef struct StudentRoster *StudentRosterP;
^
1 error generated.
无论如何都不能更改或修改StudentFloster.h文件,因为它是提供的文件,.c和其他附带的.h文件必须完全符合StudentFloster.h的描述。提前谢谢你的帮助 您需要定义类型
struct Node
和struct StudentRoster
,而不是使用指针typedefs(struct NodeP
和struct StudentRosterP
)的名称,因此下面的代码可能就是您实际的意思:
struct StudentRoster // No P
{
NodeP root;
};
struct Node // No P
{
StudentP left;
StudentP right;
};
你有:
typedef struct StudentRoster *StudentRosterP;
你还有:
struct StudentRosterP
{
NodeP root;
};
这些类型是不相关的;你需要:
struct StudentRoster // No P at the end here!
{
NodeP root;
};
冲洗并重复
(请注意,他在书中的表述与此大致相同,但可能没有如此精确或简洁地表述,至少一开始没有。)
你能解释一下为什么没有指针作为结构定义会给我一个错误吗
p
后缀是人类的习惯;编译器不知道该约定存在。当您定义struct StudentRosterP
时,仍然没有定义访问StudentRosterP thisloster内部所需的struct StudentRosterP
代码>。这定义了一个变量,该变量是指向不完整类型的指针。只要不需要访问结构中的任何内容,就可以拥有并使用此类指针。它们是“不透明类型”,非常有用。但是使用thisloster->root
试图访问不透明类型的内部,但这不起作用
我想这只是不喜欢的另一个原因,尽管我知道您对类型名束手无策。即使您声明了类型别名,它们仍然引用实际的结构。如果代码没有实际的结构,那么它就不能使用类型别名。看,嗯,我有点明白你的意思,但是请@joachimpileborg,你能在这个想法上再推断一点吗?因为标题名中有一个拼写错误(#包括“StudentRsoter.h”
),我们知道您没有向我们展示编译器看到的代码。(a) 小心点。(b) 向我们展示准确的代码,而不是近似值,因为只有近似于真实代码才能得到答案的近似值。@JonathanLeffler-指针typedef是固定不变的,我无法更改它。请解释为什么不将指针作为结构定义会给我一个错误。顺便说一句,成功了,非常感谢!