3

Possible Duplicate:
Why can templates only be implemented in the header file?
Undefined reference to function template when used with string (GCC)
C++ templates, undefined reference

I feel I'm missing something linking a C++ project. I'm not sure if the problem is in the header sources or includes so I made a minimal code sample to demonstrate it.

Main module minmain.cpp:

 #include <stdio.h>
 #include <vector>
 #include <string>
 #include "nodemin.h"

 using namespace std;

 int main()
 {
     // Blist libs are included
     Node<int>* ndNew = ndNew->Root(2);

     return 0;
 }

Header file nodemin.h:

 #ifndef NODETEMP_H_
 #define NODETEMP_H_

 using namespace std;

 template<class T>
 class Node
 {
 protected:

    Node* m_ndFather;
    vector<Node*> m_vecSons;
    T m_Content;

    Node(Node* ndFather, T Content);

 public:

     // Creates a node to serve as a root
         static  Node<T>* Root(T RootTitle);
 };

 #endif

node module nodemin.cpp:

 #include <iostream>
 #include <string.h>
 #include <vector>
 #include "nodemin.h"

 using namespace std;

 template <class T>
 Node<T>::Node(Node* ndFather, T Content)
 {
    this->m_ndFather = ndFather;
    this->m_Content = Content;
 }

 template <class T>
 Node<T>* Node<T>::Root(T RootTitle) { return(new Node(0, RootTitle)); }

Compile line:

 #g++ -Wall -g mainmin.cpp nodemin.cpp

Output:

 /tmp/ccMI0eNd.o: In function `main':
 /home/******/projects/.../src/Node/mainmin.cpp:11: undefined reference to`Node<int>::Root(int)'
 collect2: error: ld returned 1 exit status

I tried compiling into objects but the linking still failed.

Community
  • 1
  • 1

1 Answers1

0

Add template class Node<int>; to nodemin.cpp:

#include <iostream>
#include <string.h>
#include <vector>
#include "nodemin.h"

using namespace std;

template <class T>
Node<T>::Node(Node* ndFather, T Content)
{
   this->m_ndFather = ndFather;
   this->m_Content = Content;
}

template <class T>
Node<T>* Node<T>::Root(T RootTitle) { return(new Node(0, RootTitle)); }

template class Node<int>;
Ruben
  • 2,488
  • 1
  • 18
  • 22
  • 1
    While this solves *this particular* problem, it just obscures the general problem. Without any explanation about what it does I would *not* recommend this solution, and in general I doubt that it really suits OP. – Konrad Rudolph Jun 02 '12 at 18:38
  • It works. So the problem was actually trying to fit the non-class 'int' to a class template, Guess I could blame myself for using too much pure-oop. So if a make the template 'Type T' it would be completely generic and would fit non-class types too, Right? – Guy Shepherd Jun 02 '12 at 18:40
  • Look at link in comment to your question. Really, problem is caused by the fact, that compiler doesn't generate implementation for T=int while processing nodemin.cpp. Also compiler doesn't generate implementation while processing minmain.cpp (as it was not defined in this file). So, compiler didn't generate implementation for T=int at all and because of this linker cannot find it. Putting `template class Node` at end of nodemin.cpp hints compiler to generate corresponding implementation. – Ruben Jun 02 '12 at 18:45
  • @Ruben, I'm aware of the fact there's no int implementation to Node class in my code, My intention was having a 'payload' for the Node and the payload is type-independent. My follow-up question was how should I implement it in C++, If I used C# I would make the payload an object. In order to implement a generic type should I use void pointers? Or maybe make several implementations to several types of Node? – Guy Shepherd Jun 02 '12 at 19:15
  • No, you just have to put implementation in header file (to nodemin.h instead of nodemin.cpp). – Ruben Jun 02 '12 at 20:25