高手们,难道template函数只能在头文件中定义?
我的程序如下:
头文件 test.h:
template <class T>
class test
{
public:
T x;
T f();
};
test.cpp文件:
#include "test.h"
template<class T>
T test<T> ::f()
{
return x;
}
结果在main()函数中这样调用就不行
main()
{
test<float> ttt;
ttt.f();
}
编译时链接出错,说
error LNK2001: unresolved external symbol "public: float __thiscall test<float>::f(void)" (?f@?$test@M@@QAEMXZ)
但是:
如果把函数的定义写在.h中,同时去掉cpp中的定义,就一切正常了。
头文件 test.h:
template <class T>
class test
{
public:
T x;
T f(){return x;};
};
为什么???????????????