【发布时间】:2014-12-31 19:40:09
【问题描述】:
我有两个这样的头文件:
#ifndef LAYER_ONE_TREE_H
#define LAYER_ONE_TREE_H
#include "Utils.h"
#include "LayerTwoTree.h"
class LayerOneTreeNode{
public:
friend class LayerOneTree;
friend class LayerTwoTree;
.
.
.
LayerTwoTree* S_U1;// A pointer to the root of a layerTwoTree
LayerOneTreeNode(){
S_U1 = new LayerTwoTree; //here is the error Error 1 erro C2512: 'LayerTwoTree' : no appropriate default constructor available
S_U1->init();
}
};
class LayerOneTree{
public:
LayerOneTree(){
}
.
.
.
private:
.
.
.
};
#endif
和第二个标题:
#ifndef LAYER_TWO_TREE_H
#define LAYER_TWO_TREE_H
#include "Utils.h"
#include "LayerOneTree.h"
class LayerTwoTreeNode{
public:
friend class LayerTwoTree;
friend class LayerOneTree;
.
.
.
//constructor
LayerTwoTreeNode(Point v = Point(), LayerTwoTreeNode *l = nullptr,
LayerTwoTreeNode *r = nullptr, NodeColor c = Black)
: key(v), color(c), left(l), right(r)
{}
};
class LayerTwoTree{
public:
friend class LayerOneTree;
friend class LayerOneTreeNode;
.
.
.
LayerTwoTree(){
}
LayerOneTreeNode* fatherNode; //the father node of this tree
};
#endif
当我尝试在我的LayerOneTree 中使用LayerTwoTree 时,我不知道为什么会出现“没有可用的适当默认构造函数错误”。我认为问题在于我想在LayerOneTree 中有一个LayerTwoTree,在我的LayerTwoTree 中也有一个LayerOneTree。有没有办法解决这个问题?如果您需要了解有关代码的更多详细信息,请发表评论。
【问题讨论】:
-
没有可用的默认构造函数,因为此时类尚未完全定义。将类主体中的函数删除到单独的实现文件中,该文件在编译时包含两个类定义。
-
@UlrichEckhardt ,这两个头文件的实现在单独的 cpp 文件中。 “将函数从类主体中删除到单独的实现文件
compiled with both class definitions”是什么意思?你能给我举个例子吗?提前致谢
标签: c++ visual-c++ compiler-errors