1

I have 3 C++ files: genericStack.h:

template <class T> 
class Stack{
 public:
  Stack (int size){
    top = -1;
    MAX_SIZE = size;
    v = new T (size);
  }
  ~Stack(){ delete v;}

  T pop();
  void push (T);

  class Underflow{};
  class Overflow{};

  private:
   int top;
   int MAX_SIZE;
   T* v;
 };

genericStackImpl.c++:

#include "genericStack.h"

template <class T>
void Stack <T> :: push (T c){ 
  if (top == MAX_SIZE - 1) throw Overflow();
  v[++top] = c;
} 

template <class T>
T Stack <T> :: pop(){
  if (top < 0) throw Underflow();
  return v[top--];
}

driver.c++:

#include <iostream>
#include "genericStack.h"
int main(){
 Stack<char> sc(3);
 try{
   while (true) sc.push ('p');
 }
 catch (Stack<char>::Overflow){std::cout << "Overflow caught\n";}
 try{
  while (true) std::cout << sc.pop() << '\n';
 }
 catch (Stack<char>::Underflow){ std::cout << "Underflow caught\n";}
 return 0;
}

When i compile using g++ 4.5:

g++ -o driver driver.c++ genericStackImpl.c++

I get these errors:

/tmp/ccLXRXgF.o: In function `main':
driver.c++:(.text+0x2e): undefined reference to `Stack<char>::push(char)'
driver.c++:(.text+0x3c): undefined reference to `Stack<char>::pop()'
collect2: ld returned 1 exit status

I dont understand what the problem is. If i move the implementation in the driver file, then it compiles and runs.

badmaash
  • 4,775
  • 7
  • 46
  • 61

1 Answers1

3

Generally speaking, template definitions need to also be in the header file. An exception to this is when you are explicitly instantiating, or using explicit specialisations, neither of which you are doing.

One way to solve this would be to move the contents of genericStackImpl.c++ to the bottom of its header file.

The reason for this is because template functions are not actual functions, they are just templates. A template is used (instantiated) to create actual functions, and those are what you link against.

There are no functions in genericStackImpl.c++. The functions only get created once you use them, i.e. the first time the compiler sees sc.push and sc.pop. Unfortunately, when driver.c++ tries to create these functions, it can't find the template bodies -- they hidden in genericStackImpl.c++! Instead, it just compiles a reference to those functions, hoping that some other file will make them.

Finally, when it comes to link time, the linker can't find the function anywhere, so it gives you an error.

Another way to solve this would be to explicitly instantiate those functions yourself in genericStackImpl.c++, i.e.

template class Stack<char>;

This will create the functions, and the linker will find them.

The problem with this approach is that it require you to know what types your going to be using in your stack beforehand, so most people just put template definitions in the header file.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168