Thread: simple template question

  1. #1
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113

    simple template question

    thetemplate.h:
    Code:
    template<typename X>
    class temp {
     public:
      temp();
    };
    test.cpp:
    Code:
    #include "thetemplate.h"
    
    template<typename X>
    temp<X>::temp()
    {
    }

    main.cpp:
    Code:
    #include "thetemplate.h"
    
    int main()
    {
      temp<int> t;
      return 0;
    }
    g++ main.cpp test.cpp:
    Code:
    /tmp/ccaJaZBs.o(.text+0x23): In function `main':
    : undefined reference to `temp<int>::temp()'

    What's the problem?
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Heres my take on the matter forgive me if I am way off. You have a class template named temp (horrible name). It wich accepts the generic type X. Your definitionn of the constructor temp says it is of type X, but not of type temp. The compiler error is complaining because there is nothing corresponding to to essentialy a function which is of type int (in this case).

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    put the funcs in the .cpp in the .h. You need export to do that. The only compiler i know that supports export is comeau.Your template definitions must live in the same translation unit as the point of instantiation.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Quote Originally Posted by Stoned_Coder
    put the funcs in the .cpp in the .h. You need export to do that. The only compiler i know that supports export is comeau.Your template definitions must live in the same translation unit as the point of instantiation.

    Why? I always thought putting all your code in header files was bad form (and still do). What's the point of ever using cpp files if you can just put it all in a header file?

    (note to anyone who thinks I'm demonstrating lak of knowledge: yes, yes, I know, if you never include a header file in a cpp file it will never get compiled, but what I *mean* is, why not put all your code except "main" into header files? by using the template argument that would make sense but I maintain that it's bad form)

    What I really want to know is.. why on earth can't I put my definitions in a header and my implementation in a cpp file, like it should be?
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Because templates work differently than other code. You are right, you should prefer to separate interface and implementation in general, just not for templates (at least as long as compilers don't support it).

    A templated class or function is just that - a template. There is no actual code, just the template for the code for a class or function depending on the template arguments. If you have a class temp that depends on a type X, then the compiler is not going to generate the code for all possible types X that could work with temp. Instead, it will only generate the code for types that it knows will be used.

    When you use temp<int> in main, it tries to generate the code for the temp class with X = int. However, it only compiles one cpp file at a time, so at the time it is compiling main.cpp it cannot create a copy of the code in test.cpp with int as the template argument because it cannot see that code. When the linker goes to link the files together, there are no object files with the definition of temp<int>::temp() - hence the linker error.

    If you happened to use temp<int> in test.cpp, then that code would be generated and you probably wouldn't get the linker error. This isn't a good way to solve the problem, though because then for any type X that you want to use with your template class or function, you'd need to use it once in the template class cpp file. Putting everything in the header file is a better solution.

    You can also leave the interface and implementation separated into two files, and just #include the .cpp file at the bottom of the .h file. This is one common technique that fixes the linker error but keeps the code in separate files for if/when export or an alternative is supported.

  6. #6
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Quote Originally Posted by Daved
    A templated class or function is just that - a template. There is no actual code, just the template for the code for a class or function depending on the template arguments. If you have a class temp that depends on a type X, then the compiler is not going to generate the code for all possible types X that could work with temp. Instead, it will only generate the code for types that it knows will be used.

    You're right, of course.. thanks muchly for pointing that out

    I may have known that at some point but I've only ever used templates twice before, and I haven't actually been doing much coding in C++ for some time (I've been using Java, Perl, and C much more than C++ for the last couple of years, since I haven't really had any major personal projects that I was working on and my University courses have so far been mostly in Java... btw I like using C/C++ better than Java, but anyway, back to the point ).

    I suppose I'll just go ahead and put it in the header file, for now at least, since that will work just fine for my project, I just would rather have done it the other way for style's sake. Thanks for the input.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  2. Template question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 10-12-2008, 07:16 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Template Question
    By Eber Kain in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2004, 10:07 PM