   Not sure what it is that I'm doing wrong with templates in this case
  Hi, I'm having a weird problem with templates. Here is the linker error that I'm getting (in bold):% make clean && time makerm -rf *o maing++ -c -g generic-stack.cppg++ -c -g main.cppg++ -c -g stack.cppg++ -c -g enhanced-stack.cppg++ -c -g base-stack.cppg++ -c -g array-stack.cppg++ -c -g list-stack.cppg++ main.o generic-stack.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.o -o mainUndefined 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.old: symbol(s) not found for architecture x86_64collect2: ld returned 1 exit statusmake: *** [main] Error 1Here 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;};#endifHere 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.Makefileall: mainmain: generic-stack.o main.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.og++ main.o generic-stack.o stack.o enhanced-stack.o base-stack.o array-stack.o list-stack.o -o maingeneric-stack.o: generic-stack.cppg++ -c -g generic-stack.cppbase-stack.o: base-stack.cppg++ -c -g base-stack.cpparray-stack.o: array-stack.cppg++ -c -g array-stack.cpplist-stack.o: list-stack.cppg++ -c -g list-stack.cppstack.o: stack.cppg++ -c -g stack.cppenhanced-stack.o: enhanced-stack.cppg++ -c -g enhanced-stack.cppnew-stack.o: new-stack.cppg++ -c -g new-stack.cppmain.o: main.cppg++ -c -g main.cppclean:rm -rf *o mainThoughts?[edit]Yes, I googled, no I could not find an answer that helped me.   
  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  .   ;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  . 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.;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.   