Thread: Separating Header and Method Implementation

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Separating Header and Method Implementation

    I used to put my methods implementation together with class definition in the header file. I would like to separate them. The following are my new code:

    Code:
    // File: MyClass.h
    #ifndef __MYCLASS_HEADER__
    #define __MYCLASS_HEADER__
    
    template <class t>
    class MyClass {
      public:
      void doNothing(t var);
    };
    
    #endif	// __MYCLASS_HEADER__
    
    // File: MyClass.cpp
    #include "MyClass.h"
    
    template <class t>
    void MyClass<t>::doNothing(t var) {
      var = var;
    }
    
    // File: MyMain.cpp
    #include "MyClass.h"
    
    int main() {
      MyClass<int> myClass;
    
      return 0;
    }
    I got the following linking error,
    error LNK2001: unresolved external symbol "public: void __thiscall MyClass<int>::doNothing(int)" (?doNothing@?$MyClass@H@@QAEXH@Z)

    I must have missed something. Anyone can help?
    Last edited by dickyDick; 07-29-2005 at 10:34 AM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Functions of template classes have to be defined in the header. What I usually do (just to preserve the nice header-implementation seperation) is create a .tmpl file, put my template implementation in there, and then #include it inside the .h inclusion guards.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I used to combine them even for non-template classes. A while ago, as I developed my program I encountered some problem as I included a header file (non-template class) in more than 1 cpp file. Someone told me it is a bad habit to combine. As from your reply, I understand that for template classes, they have to be combined. So combine them if it is template class and separate them if it is non-template class. Is that right?

    Could you also give me some example for your suggestion above?

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Sure. I implement my template class and inline functions in exactly the same manner, since the implementation has be included in the header file. So, for a template class, I'd do something like this:
    Code:
    //in template_class.h
    #ifndef TEMPLATE_CLASS_H_JNSDF_092839
    #define TEMPLATE_CLASS_H_JNSDF_092839
    
    template <class T>
    class MyClass
    {
      public:
        void doNothing();
    };
    
    #include "template_class.tmpl"
    
    #endif
    
    //in template_class.tmpl
    template <class T>
    void MyClass<T>::doNothing()
    {
      //do stuff
    }
    
    //in main.cpp
    #include "template_class.h"
    
    int main()
    {
      MyClass<int> myClass;
      myClass.doNothing();
      return 0;
    }
    Similarly with inline code, except I use a .inl extension so that I know what kind of code I have in each file.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. Problem with header file method
    By Beowolf in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2007, 10:10 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Operator overloading,initization, Print() in header file
    By Tozilla in forum C++ Programming
    Replies: 9
    Last Post: 03-27-2003, 07:33 AM