【问题标题】:error C2512: 'LayerTwoTree' : no appropriate default constructor available错误 C2512:“LayerTwoTree”:没有合适的默认构造函数可用
【发布时间】: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


【解决方案1】:

分析:

假设某个文件包含LayerTwoTree.h,相关行是:

#ifndef LAYER_TWO_TREE_H
#define LAYER_TWO_TREE_H
#include "LayerOneTree.h"

此时,LayerOneTree.h 的内容已包含在翻译单元中:

#ifndef LAYER_ONE_TREE_H
#define LAYER_ONE_TREE_H
#include "LayerTwoTree.h"

此时,LayerTwoTree.h 的内容再次被包含在翻译单元中:

#ifndef LAYER_TWO_TREE_H
#endif

请注意,包含保护之间的所有内容都已被跳过,因为宏已经定义!所以,回到 LayerOneTree.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();
    }

...但是此时您正在尝试创建这个不完整类的实例,MSC 抱怨它并带有一些误导性的错误消息。

解决方案:

使用此架构:

// declare classes
class Foo;
class Bar;
// define classes
class Foo {
    Bar* p;
public:
    // declare ctor
    Foo();
};
class Bar {
    Foo* p;
public:
    // declare ctor
    Bar();
};
// define ctor
Foo::Foo() {
    p = new Bar();
}
Bar::Bar() {
    p = new Foo();
}

或者,搜索“循环依赖C++”,你会发现更多的解释。

【讨论】:

    猜你喜欢
    • 2016-03-28
    • 2013-08-25
    • 2012-10-10
    • 2012-12-16
    • 2014-01-31
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多