Hi! I'm running Ubuntu 9.04 and using Code::Blocks as C++ IDE. I have the following code:
Temp.h:
#ifndef TEMP_H_INCLUDED
#define TEMP_H_INCLUDED
template <typename T>
class Temp {
public:
Temp (T d = T(0)): _dato(d) {}
T getDato () const;
void setDato (T d);
private:
T _dato;
};
template class Temp<double>;
#endif // TEMP_H_INCLUDED
Temp.cpp:
#include "Temp.h"
template <typename T>
T Temp<T>::getDato () const { return _dato; }
template <typename T>
void Temp<T>::setDato (T d) { _dato = d; }
main.cpp:
#include "Temp.h"
#include <iostream>
using namespace std;
int main()
{
Temp<double> temp(3.24);
cout << temp.getDato() << endl;
return 0;
}
As you can see, I'm trying to use explicit instantiation of the template in the header file, but when I try to compile, the linker shows me "undefined reference to Temp<double>::getDato() const".
The weird thing is that this code works in Windows with MinGW and GCC. In Ubuntu, it works if I put the template instantiation on the end of the Temp.cpp file.
I know I can / should put all the template's implementation on the header file and that will work, but this is for an university's work and they ask me to separate the implementation.
I'll apreciate any help or explanation!