Please read this first:
Storing C++ template function definitions in a .CPP file
This is what I am attemptng here.
I used the first method from this site to define a template class in a cpp file:
http://www.codeproject.com/Articles/48575/How-to-define-a-template-class-in-a-h-file-and-imp
It looks like this:
b.h :
#ifndef B_H_
#define B_H_
class B
{
public:
B();
};
#endif
a.h :
#ifndef A_H_
#define A_H_
template<class T>
class A
{
public:
A();
int get();
private:
int val;
};
#endif
a.cpp
#include "a.h"
template <class T>
A<T>::A()
{
}
template <class T>
int A<T>::get()
{
return 42;
}
// No need to call this TemporaryFunction() function,
// it's just to avoid link error.
void TemporaryFunction ()
{
TestTemp<B> TempObj;
}
main.cpp
#include "a.h"
int main(int argc, char** argv){
A<B> ab;
ab.get();
}
I have to change get to virtual or I get:
undefined reference to `A::get()'
I don't understand this. This is just a sample of the real code (that is too large). If it does not reproduce, I will provide more details tomorrow.
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)