views:

28

answers:

1

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: unresolved external symbol "public: int __thiscall CTestClass::getNewValue(void)" (?getNewValue@?$CTestClass@H@@QAEHXZ) referenced in function _wmain

+2  A: 

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.

James McNellis