Thread: Implementation of a template class functions

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Implementation of a template class functions

    Everytime i add the implementation of a template class function outside of the header file, i get an "Undefined reference" linker error to that function.

    I can define the function within the template class like:
    Code:
    template <class T>
    class myClass
    {
     ...
     ...
     int myFunction(const T& something)
     {
        return 1;
     }
    };
    But if i define it outside of the header file (such as in the associated .cpp file) like this:

    Code:
    template <class T>
    int myClass<T>::myFunction(const T& something)
    {
      return 1;
    }
    i get linker errors.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    most compilers can not compile template implementations outside the header file
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the compiler needs to "see" the source code for that particular implementation when it sees the code using it, so in general, that means that you need to put all the implementations in a header file (or multiple header files).

    The C++ standard actually has support for "export" implementations, but as of right now, I'm not aware of any compiler that actually implements this. The support is extended in the C++0x standard, if I understand things right - but again, no compilers available that fully supports this standard.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by matsp View Post
    The C++ standard actually has support for "export" implementations, but as of right now, I'm not aware of any compiler that actually implements this. The support is extended in the C++0x standard, if I understand things right - but again, no compilers available that fully supports this standard.
    The export keyword in the current standard is supported, reportedly, by the Comeau compiler. Apparently the implementation of it was far from trivial, which would explain why most compilers do not support it. That observation might also account for changes to be made in the C++0x standard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  3. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  4. template functions inside a class
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2004, 10:05 PM
  5. bug for static functions in template class
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 06:38 PM