Thread: Template class and unresolved externals..!

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Question Template class and unresolved externals..!

    Hello all,

    I'm experiencing some quite annoying unresolved externals here.

    I have the following code:
    Code:
    // In Foo.h 
    #ifndef _FOO_H_
    #define _FOO_H_
    
    template <typename T> 
    class CFoo
    {
    private:
    	T		m_someT;
    public:
    	void SetSomeT(T value); // Doesn't work!
    
    	// Works, but not desirable.
    	/*void SetSomeT(T value)
    	{
    		m_someT = value;
    	}*/
    };
    
    #endif
    
    // In Foo.cpp 
    #include "Foo.h"
    
    template <typename T> 
    void CFoo<T>::SetSomeT(T value)
    {
    	m_someT = value;
    }
    
    // In Main.cpp 
    #include "Foo.h"
    
    int main()
    {
    	CFoo<int> foo;
    	foo.SetSomeT(2);
    
    	return 0;
    }
    However, I get the following unresolved external error:
    Template error LNK2019: unresolved external symbol "public: void __thiscall CFoo<int>::SetSomeT(int)" (?SetSomeT@?$CFoo@H@@QAEXH@Z) referenced in function _main

    I know one could "just" move the actual implementation of the template functions into the header (Foo.h) and everything would work out just fine, but WHY?
    I mean.. I like keeping my classes and its implementation separated (unless we're talking small accessor functions, etc.)

    Can anyone explain why this is messing up the linker?

    Oh, and while we're on it.. if I inline my functions in an ordinary class (in the .cpp, too) I get unresolved symbols until I move the actual implementation into the class definition itself. This really doesn't suit me well if I'm doing a .lib and I would like to hide the actual implementation from the user.

    Please, I hope you can enlighten me on my C++ path

    Thanks in advance!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Templates aren't actual code, they're just templates. The compiler needs to create actual code from them. This is why the compiler must have access to all template code, which means you must put it all into header files.
    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

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Or you could include both the .h file and the .cpp file.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    NO! NEVER NEVER NEVER include cpp files!
    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

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    31
    Originally posted by CornedBee
    NO! NEVER NEVER NEVER include cpp files!
    LOL.. thanks for the fast answer(s)!

    Seems I'll have to accept the template thing!
    What about the inline question? Imagine you're making a .lib and you want to hide the imlementation from the user.. how's that possible if you can't have inline functions in the .cpp file because of the unresolved symbols?

    Thanks for the super fast answers!

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You can't. It's a limitation. Inline functions are visible to the user. That's just the way it is.
    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

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I usually place the template functions in the header, but if you do't like that put them in an *.ipp file and include that.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Lightbulb

    Just to brush up on this reasonably old thread

    Quote Originally Posted by Magos
    I usually place the template functions in the header, but if you do't like that put them in an *.ipp file and include that.
    .ipp as in "include-pp file" ? How is that any better than including .cpp files?

    Also, just a quick rehap. So it is required that all template functions be specified in .h files, like in the class in which they're defined? That is, there is no way to separate the class declaration from the actual definition?

    Thanks,

  9. #9
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    In fact, so old that I'm going to ask you to make a new thread. Please read the forum rules and don't bump old threads...let them RIP.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by CornedBee
    NO! NEVER NEVER NEVER include cpp files!
    Placing all code in the header, or including the cpp is exactly 100% the same thing. Of course both cases can only be done when using templates, otherwise you'll get symbol redefinition. When using templtes I ALWAYS place all code in a cpp then include it, because then I'll get a clean headers, only with declaration, and comments explaning everything. But there's a vey important issue when including a cpp
    In the h file I write
    Code:
    #ifndef _FILE_H_
    #define _FILE_H_
    
    /*
    declarations, comments, whatever
    */
    #include "file.cpp"
    
    #endif //_FILE_H_
    in the cpp I write
    Code:
    #ifdef _FILE_H_
    /*
    templated code, inline functions
     - the part is compiled only when included from the h
    */
    #else
    #include "file.h"
    /*
    non-templated code
    */
    #endif //_FILE_H_
    This way each piece of code won't be analysed twice, and you can make sure the matching header has been included.

    during compilation, the directive #include "file.cpp" will be substituted by the contents of the cpp file, which is the same as placing all code in the h.
    But again, it's up to style. I much prefer having clean simple, and commented headers (I believe that's their porpouse), than all code mashed together.
    So you decide. This also applies to inline functions
    Last edited by xErath; 07-06-2005 at 12:25 PM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's not about including or not including additional files. I tend to include "ipp" files as well. What I take issue at is including files with the .cpp ending, because sooner or later, someone or something is going to try and compile them, and they're not made for that. The query on another file's include guard is bad on general principle - an include guard should please stay within the file it belongs to.
    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

  12. #12
    Registered User
    Join Date
    Feb 2004
    Posts
    31

    Talking

    Thanks for the helpful replies, guys. I, too, like to have clean headers and use files with other extensions, such as .cpp, to deal with the actual definitions.

    I'll come up with a style of some sort

    Quote Originally Posted by kermi3
    In fact, so old that I'm going to ask you to make a new thread. Please read the forum rules and don't bump old threads...let them RIP.
    Sorry about that. I just figured it would help make the question clearer and that it was just an extension of the original thread.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by CornedBee
    What I take issue at is including files with the .cpp ending, because sooner or later, someone or something is going to try and compile them
    That's why I use the precompiler directives - to prevent compiling/linking errors. Till now it has worked well.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by xErath
    That's why I use the precompiler directives - to prevent compiling/linking errors. Till now it has worked well.
    Oh, I'm sure it works well enough. I just think your style is confusing. And the template-in-implementation-file mistake is so common that I find my first post a VERY good rule of thumb, especially for newcomers.
    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

  15. #15
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You can't. It's a limitation. [Template] functions are visible to the user. That's just the way it is.
    This will likely be changed in the next C++ standard (C++0x). I look forward to it.

    [edit]
    As an interesting result of this quirk, it does allow you the opportunity to see your compiler's implementation of the STL in it's include libs.
    Last edited by LuckY; 07-12-2005 at 10:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  2. What could be wrong about this template?
    By kromozom in forum C++ Programming
    Replies: 10
    Last Post: 12-27-2004, 01:15 AM
  3. unresolved external, template class prob
    By *ClownPimp* in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2003, 12:55 AM
  4. unresolved externals
    By Shadow12345 in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2002, 03:15 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM