Not sure what it is that I'm doing wrong with templates in this case

Started by
17 comments, last by ysg 11 years ago
Hi, I'm having a weird problem with templates. Here is the linker error that I'm getting (in bold):
% make clean && time make
rm -rf *o main
g++ -c -g generic-stack.cpp
g++ -c -g main.cpp
g++ -c -g stack.cpp
g++ -c -g enhanced-stack.cpp
g++ -c -g base-stack.cpp
g++ -c -g array-stack.cpp
g++ -c -g list-stack.cpp
g++ main.o generic-stack.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.o -o main
Undefined symbols for architecture x86_64:
  "GENERIC_STACK<int>::GENERIC_STACK()", referenced from:
      _main in main.o
  "GENERIC_STACK<int>::~GENERIC_STACK()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
Here is the header file:
#ifndef GENERIC_STACK_HPP
#define GENERIC_STACK_HPP

#include "global-includes.hpp"

template <typename T>
class GENERIC_STACK
{
  public:
    GENERIC_STACK();
    ~GENERIC_STACK();
    void Push(T value);
    T Pop();
    bool Is_Empty();

  private:
  protected:
    struct NODE
    {
      T value;
      NODE * link;
    };

    NODE * Head;
};

#endif
Here is a source file:
#include "generic-stack.hpp"

template <typename T>
GENERIC_STACK<T>::GENERIC_STACK()
{
  Head = NULL;
}

template <typename T>
GENERIC_STACK<T>::~GENERIC_STACK()
{
  T value;

  while(!Is_Empty())
  {
    Pop(value);
  }
}

template <typename T>
void GENERIC_STACK<T>::Push(T value)
{
  NODE * temp = new NODE;
  temp->value = value;
  temp->link = Head;
  Head = temp;
}

template <typename T>
T GENERIC_STACK<T>::Pop()
{
  T value;
  NODE * temp = Head;
  value = Head->value;
  Head = Head->link;
  delete temp;
}

template <typename T>
bool GENERIC_STACK<T>::Is_Empty()
{
  return Head == NULL;
}
This is the main method:
#include "new-stack.hpp"
#include "list-stack.hpp"
#include "array-stack.hpp"
#include "generic-stack.hpp"

int main(int argc, char * argv[])
{
  ...


  GENERIC_STACK<int> int_stack;

  return 0;
}
Two things I'd like to add:
1 - I know that standards-wise, this code is ugly. This is just my temporary thing that I'm using to remember about C++.
2 - "global-includes.hpp" includes stuff like #include <iostream>, etc.

Now, my question, why am I getting this ugly error? I've checked my Makefile and that looks good (below) and I don't understand why this is happening.

Makefile
all: main

main: generic-stack.o main.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.o
	g++ main.o generic-stack.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.o -o main

generic-stack.o: generic-stack.cpp
	g++ -c -g generic-stack.cpp

base-stack.o: base-stack.cpp
	g++ -c -g base-stack.cpp

array-stack.o: array-stack.cpp
	g++ -c -g array-stack.cpp

list-stack.o: list-stack.cpp
	g++ -c -g list-stack.cpp

stack.o: stack.cpp
	g++ -c -g stack.cpp

enhanced-stack.o: enhanced-stack.cpp
	g++ -c -g enhanced-stack.cpp

new-stack.o: new-stack.cpp
	g++ -c -g new-stack.cpp

main.o: main.cpp
	g++ -c -g main.cpp

clean:
	rm -rf *o main
Thoughts?

[edit]

Yes, I googled, no I could not find an answer that helped me.
Advertisement
Long story short, you can't separate template definitions into their own source file (without special compiler support that almost no compiler has, or unless you what to manually instantiate the templates for each type you want to use). Put the template functions into the header file.
So put the source code in the header file?

So put the source code in the header file?

That is the common solution. The other solution as mentioned is to do something like:


template GENERIC_STACK< int >;

in the source to force implementation of a specific version of the template. (I believe I have that correct, rarely implement a full class that way, just members.)

PS. this is not a good idea in general, putting the code in the header to be generated at compile time is better than forcing a compile time generation of the entire class.


So put the source code in the header file?


That is the common solution. The other solution as mentioned is to do something like:


template GENERIC_STACK< int >;

in the source to force implementation of a specific version of the template. (I believe I have that correct, rarely implement a full class that way, just members.)

PS. this is not a good idea in general, putting the code in the header to be generated at compile time is better than forcing a compile time generation of the entire class.


Yeah, I'd like to avoid that if I can smile.png .


PS. this is not a good idea in general, putting the code in the header to be generated at compile time is better than forcing a compile time generation of the entire class.

Yeah, I'd like to avoid that if I can smile.png .

You get a gold star for that. I used to use ".inl" files to separate the class definition in the standard 'header' file from the 'inline' file, but Xcode ignores the '.inl' files and you can't debug through them or set breakpoints, so you have to use 'h' or 'hpp' on that platform. (I'm sure you can customize it, I refuse to do so though, might make things nice for me but others working with the codebase may not have all the customizations) Of course, it is non-standard and as such probably not a great thing to do, but it was nice. I could put the declaration of the interface in an hpp, the inline function definitions in a inl and any non-template parameter specific stuff in a cpp where I used the explicit definition as posted above to make the compiler generate the code.

Not to try and prove you all wrong or anything, but I found this article a while ago which says that it is possible to separate the declarations and definitions for template functions into .h and .cpp files. It does seem though (from the sample compiler/linker messages he provides) that he's using MSVC rather than GCC.

I use templates in some of my projects and have been using "Method 1" (or as he writes, "Mehtod 1") to get around linking errors. Although, I too use only MSVC (haven't gotten a chance to learn how to use GNU tools yet).

EDIT: Fixed typo.

Some favourite quotes:Never trust a computer you can't throw out a window.
- Steve Wozniak

The best way to prepare [to be a programmer] is to write programs, and to study great programs that other people have written.
- Bill Gates

There's always one more bug.
- Lubarsky's Law of Cybernetic Entomology

Think? Why think! We have computers to do that for us.
- Jean Rostand

Treat your password like your toothbrush. Don't let anybody else use it, and get a new one every six months.
- Clifford Stoll

To err is human - and to blame it on a computer is even more so.
- Robert Orben

Computing is not about computers any more. It is about living.
- Nicholas Negroponte

I use templates in some of my projects and have been using "Method 1" (or as he writes, "Mehtod 1") to get around linking errors. Although, I too use only MSVC (haven't gotten a chance to learn how to use GNU tools yet).

It looks like "Method 1" is just a backward way to get explicit template instantiation (as mentioned by SiCrane). I think the usual syntax would be something like:


template class GENERIC_STACK<int>;

Edit: And "Method 3" is generally done like AllEightUp described (all code ends up in the header)...

Oh, any real difference between say template <typename T> and template <class T>? I tend to use the former by habit.

Not to try and prove you all wrong or anything, but I found this article a while ago which says that it is possible to separate the declarations and definitions for template functions into .h and .cpp files. It does seem though (from the sample compiler/linker messages he provides) that he's using MSVC rather than GCC.

I use templates in some of my projects and have been using "Method 1" (or as he writes, "Mehtod 1") to get around linking errors. Although, I too use only MSVC (haven't gotten a chance to learn how to use GNU tools yet).

EDIT: Fixed typo.

As ugly as it is, Method #3 is probably the best possible solution.

This topic is closed to new replies.

Advertisement