c++ - 我的 .h 文件有问题

标签 c++ header include

这是我的两个类,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/

相关文章:

c++ - Mongodb C++ 驱动程序 query() 方法未获得应有的所有结果

c - 包含文件的图表

spring - 通过过滤器向请求添加自定义 header

C++ 库包含

c++ - 错误 LNK2001 : unresolved external symbol when trying to use source and header files from another directory than the default in Visual Studio 2015

c++ - 如何从参数包中获取所有参数的类型?

c++ - pragma warning(push, 0) 不禁用/墙警告?

c++ - 掷骰子,2 个骰子,c++,意外结果

css - style.css 在 wordpress 中不起作用

C++ 质量-#include