Linking problems template with return
I'm having some problems on returning the parameter of a method as a template, look:
// CTestClass.h
template<class T>
class CTestClass
{
public:
T getNewValue();
};
// CTestClass.cpp
template<class T>
T CTestClass<T>::getNewValue()
{
return 10; // just for tests I'm returning hard coded 10
}
// main.cpp
int _tmain(int argc, _TCHAR* argv[])
{
CTestClass<int> s;
int a = s.getNewValue();
return 0;
}
I got the following error:
error LNK2019: unres开发者_如何转开发olved external symbol "public: int __thiscall CTestClass::getNewValue(void)" (?getNewValue@?$CTestClass@H@@QAEHXZ) referenced in function _wmain
You'll want to read the C++ FAQ "Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?"
Effectively, you need to define CTestClass<T>::getNewValue()
in the header file.
精彩评论