c++ - 模板类 : No default constructor

标签 c++ templates constructor

我知道有一百万篇关于这个的帖子,但我仍然不明白为什么这不起作用 =/

这一行:

test = new Test2<Test>;

给我这个错误:

error C2512: 'Test2<PARENT>' : no appropriate default constructor available
with
[
    PARENT=Test
]

代码:

template<class PARENT>
class Test2;

////////////////////////////

class Test
{
public:
    Test2<Test> *test;

    Test()
    {
        test = new Test2<Test>;
    }
};

/////////////////////////////

template<class PARENT>
class Test2
{
public:
    PARENT *parent;
};

////////////////////////////

有人可以帮帮我吗?

最佳答案

在实例化点(即在 Test 构造函数内),编译器目前所有的是 Test2<>前向声明 ;它尚不知道可用的构造函数。

要解决,要么移动Test2<>的定义之前 Test , 或移动 Test 的定义类定义之外的构造函数(和 Test2<> 的定义之后)。

关于c++ - 模板类 : No default constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5990551/

相关文章:

c++ - chdir 到 C++ macOS 应用程序中 .app 包的位置

c++ - C++问题中的选择排序

c++ - 使用 lambda 访问成员函数内的类模板参数类型失败

c++ - 使用 boost 检索当前世纪

c++ - 使用派生类时出现段错误

templates - 为什么 Magento 中的局部变量有一个下划线前缀?

c++ - 根据给定的 typedef 调用 std::to_string 或 std::to_wstring

java - 在抽象基类中使用 Java 泛型

java 。如何使用私有(private)构造函数覆盖类中的方法

c++ - 为vector的删除函数实现一个拷贝构造函数