Thread: Templates & Linkers

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Question Templates & Linkers

    I'm self-teaching Templates, and I think I've gotten a grasp on the concept (doesn't seem too, too tricky) but the problem lies when I try to compile, I'm trying this, that and the other thing, and came across what I believe to be a linking problem, as this code is completely functional when it's all in the same source file.

    But the moment I break it up into it's components below, I get compiler errors. Code and errors follow.

    SomeClass.h
    Code:
    template <class newType>
    class SomeClass
    {
            newType * obj;
            public:
                    SomeClass( void );
                    ~SomeClass( void );
    };
    SomeClass.cpp
    Code:
    #include <iostream>
    #include "SomeClass.h"
    
    template <class newType>
    SomeClass<newType>::SomeClass( void )
    {
            obj = new newType;
    }
    
    template <class newType>
    SomeClass<newType>::~SomeClass( void )
    {
            if ( !obj )
                    delete obj;
    }
    SCMain.cpp
    Code:
    #include <iostream>
    #include "SomeClass.h"
    
    int main ( )
    {
            SomeClass<char> * tsc = new SomeClass<char>();
            delete tsc;
    }
    Compiler
    Code:
    luser@servar:~/> c++ -o main SCMain.cpp SomeClass.cpp
    /tmp/ccUiG2LS.o: In function `main':
    SCMain.cpp:(.text+0x9a): undefined reference to `SomeClass<char>::SomeClass()'
    SCMain.cpp:(.text+0xd8): undefined reference to `SomeClass<char>::~SomeClass()
    collect2: ld returned 1 exit status
    I do recognize that I'm allocating memory only to delete it, this is a trial for something larger I intend to write, should I get it working, but I was never given a good reference on linkers (and how they work) Any pointers or assistance given would be most appreceated.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    template class implementation should be done in the header file for most now-days compilers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Resolved. (and I've bookmarked that FAQ) Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM