I have a class Node with a template
this is header file
// node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
using namespace std;
template <typename T>
class Node
{
private:
T content;
Node<T> *next;
public:
Node();
Node(T);
template <typename Y>
friend ostream &operator <<(ostream &, const Node<Y> &);
};
#endif // NODE_H
and that's the implementation
// node.cpp
#include "node.h"
using namespace std;
template<typename T>
Node<T>::Node()
{
content = 0;
next = NULL;
}
template<typename T>
Node<T>::Node(T c)
{
content = c;
next = NULL;
}
template<typename Y>
ostream &operator<<(ostream &out, const Node<Y> &node)
{
out << node.content;
return out;
}
and here is the main file
// main.cpp
#include <iostream>
#include "node.cpp"
using namespace std;
int main()
{
Node<int> n1(5);
cout << n1;
return 0;
}
this way, it works perfectly, but i don't think it's good to include cpp file.
so i included node.h but i get this errors
pathtofile/main.cpp:8: error: undefined reference to `Node<int>::Node(int)'
pathtofile/main.cpp:10: error: undefined reference to `std::ostream& operator<< <int>(std::ostream&, Node<int> const&)'
I really want to follow best practices, and i always separate code declaration from implementation; but this my first time working with templates.