undefined reference to template function

This is a discussion on undefined reference to template function within the C++ Programming forums, part of the General Programming Boards category; I have a class that has several template functions, designed to take a std::vector<T>& parameter, where T is intended to ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348

    undefined reference to template function

    I have a class that has several template functions, designed to take a std::vector<T>& parameter, where T is intended to be a boost::tuple type. the class is defined in a .h file, and the code is in a .cpp file. when I try to call the function with a vector of tuples, the linker complains about an undefined reference to a function with the parameter types I gave it. my question is: does the code for the template functions need to be in the .h file with the rest of the class definition, or can it be the way I have it now?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If your class has template functions, they need to be in a header file. Read this for the reason why.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2,348
    I seem to recall that they intend to change that in c++0x, and allow instantiation of templates from other translation units. that will be nice... no more inflated header files. in order to solve this problem, my header went from 300 lines to over 1200, but at least it works, so I won't complain too loudly.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can put the implementations in a separate file and include it from the header, so as to keep the physical size of the header small and its contents cleaner.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    Quote Originally Posted by Elkvis View Post
    I seem to recall that they intend to change that in c++0x, and allow instantiation of templates from other translation units. that will be nice... no more inflated header files. in order to solve this problem, my header went from 300 lines to over 1200, but at least it works, so I won't complain too loudly.
    No, that already exists with the export keyword. But very very few compilers actually implement it. I only know of Cameu (I think that's how it's spelled?).
    You can also separate template functions / classes. Declarations and definitions/implementations and put them in separate files.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No new attempt at separated template implementations is planned for C++0x. The export keyword in C++03 is a dismal failure because the implementation is incredibly complicated.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library troubles with makefile
    By mslate in forum Linux Programming
    Replies: 17
    Last Post: 07-23-2009, 04:43 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 01:00 AM
  5. I need help with templates!
    By advocation in forum C++ Programming
    Replies: 6
    Last Post: 03-26-2005, 08:27 PM

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