Thread: Template trouble

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    27

    Template trouble

    I have the following class

    Code:
    // ZLine.h
    
    #ifndef _ZLINE_H
    #define _ZLINE_H
    
    namespace Z_GLC {
    
    template<typename T> class ZLine
    {
        public:
            ZLine(T x1, T y1, T x2, T y2);
    
           double DistanceFromPoint(T x, T y);
        
        private:
           T a, b, c;
    };
    
    } // namespace
    
    #endif
    Code:
    // ZLine.cpp
    
    #include "ZLine.h"'
    
    namespace Z_GLC {
    
    template<typename T> ZLine<T>::ZLine(T x1, T y1, T x2, T y2)
    {
        // implementation
    }
    
    template<typename T> double ZLine<T>::DistanceFromPoint(T x, T y)
    {
        // implementation
    }
    
    } // namespace
    When I try to use the class I get link errors. How to deal with this issue?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The functions of a template class should go in the header file (or at least, they should be "#include"'ed, not linked)...

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    You mean something like this ?

    Code:
    // ZLine.h
    
    #ifndef _ZLINE_H
    #define _ZLINE_H
    
    namespace Z_GLC {
    
    template<typename T> class ZLine
    {
        public:
            ZLine(T x1, T y1, T x2, T y2);
    
           double DistanceFromPoint(T x, T y);
        
        private:
           T a, b, c;
    };
    
    } // namespace
    
    #include "ZLine.cpp"
    
    #endif
    OFF TOPIC: Why can't the functions of a template class be linked?

    Thanks for your help.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Phenom View Post
    You mean something like this ?

    Code:
    // ZLine.h
    
    #ifndef _ZLINE_H
    #define _ZLINE_H
    
    namespace Z_GLC {
    
    template<typename T> class ZLine
    {
        public:
            ZLine(T x1, T y1, T x2, T y2);
    
           double DistanceFromPoint(T x, T y);
        
        private:
           T a, b, c;
    };
    
    } // namespace
    
    #include "ZLine.cpp"
    
    #endif
    OFF TOPIC: Why can't the functions of a template class be linked?

    Thanks for your help.
    Yes like that, but I wouldn't call it ".cpp" then. Call it ".hpp"; that's more common practice, as .cpp files should never be included.

    Why it can't be done? Well, there exist compilers I think that can do it but it's extremely unportable. Because it's just very hard to implement, as it makes the .o file a lot harder, as it would need to store a lot more information than it does now.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #ifndef _ZLINE_H
    #define _ZLINE_H
    Nit... This violates the standard regarding reserved identifiers I believe:
    7.1.3 Reserved identifiers
    Each header declares or defines all identifiers listed in its associated subclause, and
    optionally declares or defines identifiers listed in its associated future library directions
    subclause and identifiers which are always reserved either for any use or for use as file
    scope identifiers.
    — All identifiers that begin with an underscore and either an uppercase letter or another
    underscore are always reserved for any use.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Why can't the functions of a template class be linked?
    Please see this link: templates, at the bottom of the page see the section titled: "Templates and multiple-file projects"

    Jim

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    Just curious, which compilers can link template classes?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Phenom View Post
    Just curious, which compilers can link template classes?
    I've read once, if I remember correctly, that VC++ can do it. But I haven't tried it or verified it any way, and it was ages ago so I might remember wrong.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No compilers can link template classes (a compiler is a different thing from a linker).

    g++ (the C++ compiler in the GNU compiler family) does support a feature it documents as "external template instantiations", which allow template functions and classes to be instantiated in a different source file from the source file that uses instances of the template - without needing to include the template source. Note this option is not enabled by default, but it has been supported since the mid 90s. By default, the technique described above (include the template source in the corresponding header) needs to be used.

    Several versions of Borland's C++ compilers (which are used within suites like C++ Builder) support a similar behaviour.

    Comeau C++ and (if memory serves) Sun Studio compilers support the export keyword (which is, approximately, the way the 1998 C++ standard sought to have such things done). However, the implementation of the "export" capability proved very complex and the benefits relatively small, so other compilers have not supported it. The export keyword will be either removed from the next C++ standard or deprecated (which is standardese for saying "obsolete and scheduled for removal from a future version of this standard").
    Last edited by grumpy; 12-02-2010 at 06:03 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    Another question:

    I have this:

    Code:
    template<typename T> class ZLine
    {
         public:
             template<typename V> double DistanceFromPoint(std::vector<V> v);
    
        private:
            T a, b, c;
    
    };
    I try to write the implementation by typing:

    Code:
    template<typename T, typename V> double ZLine<T>::DistanceFromPoint(std::vector<V> v)
    {
        // implementation
    }
    However I get errors. What's the right way to do this?

  11. #11
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    If I remember correctly:

    Code:
    template<class T>
    template<class V>
    double ZLine<T>::DistanceFromPoint(std::vector<V> v) 
    {
        ...
    }
    I do question your passing a vector by value and not by (const) reference, though.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  12. #12
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    Thank you for helping, it works!

    I do question your passing a vector by value and not by (const) reference, though.
    In the actual code I don't use vectors. I just put it there to make more clear what I' m trying to do.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Roni's question is valid, however. For composite types (structs, classes) it is often better to pass by const reference than by value. Passing by value copies the complete object, and is not a good idea for objects with significant data that must be copied or for which the act of copying or cleanup is expensive (non-trivial constructors and destructors)
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Template Trouble - Brainache Please Help
    By SpaceCadet in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2006, 08:51 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM