这是我的两个类,Node 和 DobleNode,它们都在不同的 .h 文件中,并且它们都有自己的 .cpp 文件。
//"Node.h"
class Node
{
public:
Node(string pName, string pID);
void setReferencia(DobleNode *pReferencia);
DobleNode* getReferencia(void);
private:
string Name;
string ID;
DobleNode *referencia;
};
//"DobleNode.h"
class DobleNode
{
public:
DobleNode(string pBank_code, string pCard_type);
void setReferencia(Node *pReferencia);
Node* getReferencia(void);
private:
string bank_code;
string card_type;
Node *referencia;
};
问题是我需要一个引用。在类Node中,必须有一个类型为DobleNode的属性,在类DobleNode中必须有一个类型为Node的属性。这似乎真的很简单,我只需要在“Node.h”之前包含“DobleNode.h”,一切都会正常......
但是如果我这样做,稍后,当我尝试编译我的小程序时,它会说标识符 Node 不存在。如果我用另一种方式来做,它会说同样的事情,但这次标识符 DobleNode 是不存在的。
我该如何解决这个问题,我认为一个解决方案可能是将两个类放在同一个文件中,但我真的认为有更好的方法来解决这个问题。 有没有办法“告诉”编译器同时检查“Node.h”和“DobleNode.h”,或者什么?
感谢您的回答。
顺便说一句,我正在使用 Visual Studio 2010 Proffesional,C++(显然)。
最佳答案
您可以前向声明一个类,因为您使用的是指针。
//"DobleNode.h"
class Node; // DECLARED! "Node.h" doesn't need to be included.
class DobleNode
{
...
和
//"Node.h"
class DobleNode; // DECLARED! "DobleNode.h" doesn't need to be included.
class Node
{
...
关于c++ - 我的 .h 文件有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15693663/