[英]declaring a struct in another struct
in my header file: 在我的头文件中:
private:
struct movieNode {
string title;
castNode *castHead;
movieNode *prev;
movieNode *next;
};
struct castNode {
string name;
castNode *next;
};
movieNode *head;
movieNode *last;
but the compiler error is: 但是编译器错误是:
expected ';'
预期的';' before '*' token
在“ *”令牌之前
my aim is that every movieNode should have a title and a cast list (with castNode). 我的目标是每个movieNode应该有一个标题和一个演员表(带有castNode)。 thanks in advance.
提前致谢。
movieNode
needs to at least be able to see an incomplete type called castNode
. movieNode
至少需要能够看到一个称为castNode
的不完整类型。 At the moment, the compiler is going "Huh, castNode
? What the hell is that?" 此刻,编译器正在运行“
castNode
, castNode
?那是什么castNode
?” because it hasn't seen the definition of castNode
yet. 因为它还没有看到
castNode
的定义。 You can avoid it in this case by simply defining castNode
before you define movieNode
. 你可以通过简单地定义避免在这种情况下
castNode
定义之前 movieNode
。 Just swap the two structs around. 只需交换两个结构。
In other cases, where you have a cyclic dependency (if castNode
had a pointer to a movieNode
too, for example), you can use a forward declaration to provide an incomplete type (it would look like class castNode;
) and then define it properly later. 在其他情况下,如果你有一个循环依赖(如果
castNode
有一个指向movieNode
太,例如),您可以使用预先声明,以提供完整类型(它看起来像class castNode;
然后正确地定义它后来。
private:
struct castNode; // <- add this
struct movieNode
{
string title;
castNode* castHead;
movieNode* prev;
movieNode* next;
};
struct castNode
{
string name;
castNode* next;
};
movieNode* head;
movieNode* last;
Declare castNode
before movienode
. 在
castNode
之前声明movienode
。
Only then movienode
"knows" what a castNode
is. 只有到那时,
movienode
知道castNode
是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.