Thread: Class Templates Again

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    Class Templates Again

    Hey Everybody. I have a question. I have a code like this in my .cpp file of the class:
    Code:
    template <typename T>
    int myClass<T>::getNumber()
    {
    return 1;
    }
    In my .h file I have everything set up correctly for the template class. However, when I compile it I get this error:

    myClass.cpp: error: no âint myClass<T>::getNumber()â member function declared in class âmyClass<T>â
    myClass.cpp: error: template definition of non-template âint myClass<T>::getNumber()â

    I do know however, that I DO have the getNumber in my .h file. Any ideas?

    Thank You.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Did you declare a getNumber() member function in myClass?

    Generally, with template classes everything goes into the header, your function definitions would just go at the bottom after the class declaration.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Never use implementation files (.cpp) when dealing with templates. Put the whole code (which deals with templates) into a .h file.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    I declared the getNumber in myClass. Also, I include a .cpp in my client program. Any ideas?

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by Desolation View Post
    Never use implementation files (.cpp) when dealing with templates. Put the whole code (which deals with templates) into a .h file.
    *coughs*

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Huh! Once I did as you guys said everything compiles now. Just one thing: I thought that it was always a bad practice to put code in the .h file. Is this an exception, and are there alternatives (ie before I was including a .cpp file). I am not challenging your correctness, just wondering.

    Thank you guys for helping me.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I thought that it was always a bad practice to put code in the .h file.
    It is generally bad practice, but not always. Templates are different, they almost have to be included in the code that calls them so that the compiler can generate code for the type you are using for the template type.

    Some people leave the code in a cpp file and then #include that cpp file at the bottom of their header file. If you do that you can make it a .inl file instead of .cpp to indicate that it isn't a normal source file.

    More information is here: http://www.parashift.com/c++-faq-lit...html#faq-35.12

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm adding this as a FYI only because it is widely criticized and not widely implemented by compilers but the export keyword is used to counter that issue. Using either Daved's suggestion or mine is a better alternative, however.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Quote Originally Posted by Desolation View Post
    I'm adding this as a FYI only because it is widely criticized and not widely implemented by compilers but the export keyword is used to counter that issue. Using either Daved's suggestion or mine is a better alternative, however.
    Thank You!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM