开发者

Linking problems template with return

开发者 https://www.devze.com 2023-01-14 20:32 出处:网络
I\'m having some problems on returning the parameter of a method as a template, look: // CTestClass.h template<class T>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消