Template linker error

This is a discussion on Template linker error within the C++ Programming forums, part of the General Programming Boards category; I'm getting an undefined reference linker error with the following. I understand that the object code is not being generated ...

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Template linker error

    I'm getting an undefined reference linker error with the following. I understand that the object code is not being generated but I'm not sure how to change that.

    templ_main.cpp
    Code:
    #include "templ.h"
    
    int main (void)
    {
      print(1);
      print('c');
      print("Hello World");
      return 0;
    }
    templ.h
    Code:
    #ifndef TEMPL_H_
    #define TEMPL_H_
    template <typename T> void print(T);
    #endif
    templ.cpp
    Code:
    #include "templ.h"
    #include <iostream>
    
    template <typename T> void print(T var)
    {
      std::cout<<var<<endl;
    }
    Any ideas on how to force the compiler to generated the needed object code?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Put declaration and definition in the same header file.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think you have to put the declaration and definition in the same file. There also may be a compiler-dependent way to make it work in separate files if I remember correctly...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No other way? If so that is just another reason for me to hate c++ (having to put code inside a header file)

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I looked back through posts, and it sounds like there is a keyword export in some versions of MSVC...I don't know what compiler your using though
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    g++, but I'll give it a try. Thanks

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Yeah I know it sucks having to do that. The way I and many other people do it is to put the implementation in a "inl" file and then include that in the bottom of the header. That way your header files aren't all cluttered and you kind-of have the implementation separate.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks all

    Hate_for_cpp++;

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    You don't typically "link" to templated functions or types. The template "instantiation" is done at compile time.
    So by the time the linker see's anything, code for print(double), print(int), print(Ojbect), etc... has already been generated and compiled by the compiler.

    >> If so, that is just another reason for me to hate c++
    You can orginize your code however your want. Just know that any template you use must of already been seen by the compiler or will be seen before the compiler finishes the current unit (see edit).
    Go open up your <iostream> and the files that it includes....99% of it is implemented right there in the headers.

    [EDIT]
    Unless you have the Comeau compiler....
    [/EDIT]

    gg
    Last edited by Codeplug; 04-18-2004 at 06:42 PM.

  11. #11
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    hmm, I have g++ and it isnt giving me troubles at all with templating outside of the header. The only thing it I am sillyI am sillyI am sillyI am sillyI am sillyes at is constructor implementation has to be done in the header file , in the class declaration no less/

  12. #12
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    >> templating outside of the header.
    What exactly does that mean?

    You can put templates anywhere you want. For most compilers, template functions and types are fully resolved and instantiated when the unit is compiled.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21