Implementation of a template class functions

This is a discussion on Implementation of a template class functions within the C++ Programming forums, part of the General Programming Boards category; Everytime i add the implementation of a template class function outside of the header file, i get an "Undefined reference" ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    most compilers can not compile template implementations outside the header file
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,261
    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, 04:01 PM
  2. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2006, 11:00 PM
  3. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 09:04 AM
  4. template functions inside a class
    By Trauts in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2004, 09: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

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