【发布时间】:2017-11-04 04:37:51
【问题描述】:
我在 C++ 中得到了以下代码:
class Level;
class Node
{
char letter;
std::string path[2];
Node *next;
Level *nlevel;
public:
Node()
{
path[0] = "";
path[1] = "";
next = NULL;
nlevel = NULL;
}
Node(char l, unsigned int h)
{
letter = l;
path[0] = "";
path[1] = "";
next = NULL;
nlevel = NULL;
nlevel->height = h;
}
virtual ~Node();
};
class Level
{
std::list<Node> vnodes;
unsigned int height;
public:
Level();
virtual ~Level();
};
调用或声明类的正确方法是什么?我一直在阅读 this 并且我已经尝试将 class Level; 放在类 Node 之前,但我得到了一个前向声明错误,如果我将每个类分隔在不同的文件中以便稍后包含它,我无论如何都会得到一个错误,因为它们相互依赖,那么应该如何声明呢?
【问题讨论】:
-
你得到什么“前向声明错误”?
-
使用您发布的代码将
class Level放在Node之前对我来说效果很好。 -
@Galik 无效使用不完整类型'class Level'
-
是你贴的代码造成的还是缺少一些代码?
-
@Galik 基本上是因为我贴的代码。
标签: c++ class forward-declaration