C++类模板构造函数问题

来源:百度知道 编辑:UC知道 时间:2024/06/15 07:58:53
template<class T1>
class base {
public:
base();
};

base<class T1>::base()
{
//这个空的构造函数如果放在里面就不会出错, 为什么?
}

int main( void )
{
base<int> a1; //编译出错, 为什么?
}

VC6出错信息:
test.obj : error LNK2001: unresolved external symbol "public: __thiscall base<int>::base<int>(void)" (??0?$base@H@@QAE@XZ)
Debug/testcpp.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

template<class T1>
class base {
public:
base();
};

template<class T1>
base<T1>::base()
{
//这个空的构造函数如果放在里面就不会出错, 为什么?
}

int main( void )
{
base<int> a1; //编译出错, 为什么?
}

template<class T1>
class base {
public:
base();
};

template<class T1>//类外定义函数要加上这一行,否则编译出错
base<T1>::base() //<>里面不用关键字class 只要虚拟类型名
{
}

int main( void )
{
base<int> a1;}
顶一楼的,是你的形式写错了,我也复习了一下

template<class T1>//加入此行,模块外定义需要再此声明模块
base<class T1>::base()
{
//这个空的构造函数如果放在里面就不会出错, 为什么?

}
或者去掉构造函数的声明及定义,由系统自己产生默认的构造函数