我知道有一百万篇关于这个的帖子,但我仍然不明白为什么这不起作用 =/
这一行:
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/