[英]Template class: No default 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<>
的前向声明 ; 它还不知道有哪些构造函数可用。
要解决这个问题,要么在 Test
之前移动Test2<>
的定义,要么将Test
构造函数的定义移到类定义之外(以及在 Test2<>
的定义之后 )。
对我来说,你的代码给出(正确地,恕我直言)错误:
invalid use of incomplete type 'struct Test2<Test>'
这与g ++ 4.5.1有关。 在你说的时候:
test = new Test2<Test>;
Test2尚未定义,仅向前声明。
线路test = new Test2<Test>;
正在Test的默认构造函数中执行。 这一行将调用没有参数的默认构造函数/构造函数。 调用上述语句时,Test的构造函数仍然不完整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.