Jump to content
  • Advertisement
Sign in to follow this  
ysg

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

This topic is 1904 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. Edited by ysg

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Hiwas

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.

Edited by Hiwas

Share this post


Link to post
Share on other sites


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 .

Share this post


Link to post
Share on other sites
Guest Hiwas


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.

Share this post


Link to post
Share on other sites

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.

Edited by Josh Vega

Share this post


Link to post
Share on other sites

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)...

Edited by kloffy

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!