p.h
#ifndef NODE
#define NODE
#include <iostream>
template <class T>
class Node {
public:
Node (T e);
T& getKey();
private:
T _key;
};
#endif
p.cc
#include "p.h"
template <class T>
Node<T>::Node(T e){
this->_key = e;
}
template <class T>
T& Node<T>::getKey(){
return this->_key;
}
main.cc
#include <iostream>
#include "p.h"
using namespace std;
int main () {
Node<int> n (100);
std::cout << n.getKey();
return 0;
}
g++ --std=c++0x -o main main.cc p.cc
/tmp/ccrIX2he.o: In function `main':
main.cc:(.text+0x19): undefined reference to `Node<int>::Node(int)'
main.cc:(.text+0x25): undefined reference to `Node<int>::getKey()'
I can't instantiate any object from main, but if I include p.cc from main it works or simply if I put together all files also works. How can I make it work this way?
Thanks in advance