Template class and unresolved externals..!
Hello all,
I'm experiencing some quite annoying unresolved externals here.
I have the following code:
Code:
// In Foo.h
#ifndef _FOO_H_
#define _FOO_H_
template <typename T>
class CFoo
{
private:
T m_someT;
public:
void SetSomeT(T value); // Doesn't work!
// Works, but not desirable.
/*void SetSomeT(T value)
{
m_someT = value;
}*/
};
#endif
// In Foo.cpp
#include "Foo.h"
template <typename T>
void CFoo<T>::SetSomeT(T value)
{
m_someT = value;
}
// In Main.cpp
#include "Foo.h"
int main()
{
CFoo<int> foo;
foo.SetSomeT(2);
return 0;
}
However, I get the following unresolved external error:
Template error LNK2019: unresolved external symbol "public: void __thiscall CFoo<int>::SetSomeT(int)" (?SetSomeT@?$CFoo@H@@QAEXH@Z) referenced in function _main
I know one could "just" move the actual implementation of the template functions into the header (Foo.h) and everything would work out just fine, but WHY?
I mean.. I like keeping my classes and its implementation separated (unless we're talking small accessor functions, etc.)
Can anyone explain why this is messing up the linker?
Oh, and while we're on it.. if I inline my functions in an ordinary class (in the .cpp, too) I get unresolved symbols until I move the actual implementation into the class definition itself. This really doesn't suit me well if I'm doing a .lib and I would like to hide the actual implementation from the user.
Please, I hope you can enlighten me on my C++ path ;)
Thanks in advance!