In a little project of mine I have situation like this:
// Header file
template <typename T>;
class AClass
{
public:
virtual void doSomething() const;
private:
std::vector<T> *v;
};
// separate cpp file
template <typename T>
void AClass<T>::doSomething() const
{
// use the vector here
}
// main.cpp
#include "AClass.h"
int main()
{
AClass<int> anObject;
anObject.doSomething();
return 0;
}
This piece of code compiles fine but is unable to link because in the main file the linker is unable to find an implementation for AClass unless I add this to the cpp file:
template class AClass<int>;
But that is just stupid, because it takes away the advantage of using templates.
Now the question: Is there a way to solve the linking problem without using the class forwarding in the cpp file or is this just impossible to have something like this in a separate .h file and .cpp file? Putting the forward class declaration is not an option for me.
I'd really like to have some clarification on this.
Thanks