Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Issues when placing functions from template class into seperate .cpp file [C++]

    I'm having an odd problem writing some template classes and placing the implementation in a separate .CPP file - specifically I keep getting the following errors:
    Error 1 error C2143: syntax error : missing ';' before 'List<T>::begin'
    Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    Here is my dummy test code:
    (proof of concept)

    -= HEADER FILE =-
    Code:
    template<typename T> class List
    {
    public:
    	class A
    	{
    	};
    
    	A begin() const;
    };

    -= IMPLEMENTATION FILE =-
    Code:
    template<typename T> A List<T>::begin() const
    {
    	return A();
    }
    Now, if I place this code directly into the header (.h) file it compiles without any errors, I only encounter issues when placing my functions within the separate implementation (CPP) file...

    Any clues would be much appreciated.
    Thanks,

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    See: http://www.experts-exchange.com/arti...mentation.html

    But I personally think when it comes to templated classes, putting it all in the header file is nicer because they are usually generic objects that get reused and having one file is easier.
    Last edited by oracleguy; 07-29-2009 at 06:35 AM.
    OracleGuy

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    74
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sounds good to me, and I don't mind keeping everything within the header (that much) but I would still like to segment the implementation - like moving it to the bottom (as Alex F recommended) - but it still doesn't work...

    If I include everything in the single .h file (as shown below) I get a new set of errors:
    error C2143: syntax error : missing ';' before 'List<T>::begin'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    As well as the following warning
    warning C4346: 'List<T>::A' : dependent name is not a type

    Code:
    template<typename T> class List
    {
    public:
    	class A
    	{
    	};
    
    	A begin() const;
    };
    
    template<typename T> List<T>::A List<T>::begin() const
    {
    	return A();
    }
    It really only seems to work if I put the implementation directly in the class itself... which makes the class defintion rather busy and hard to read...

    Any clues what could be going wrong?
    Thanks,

  • #4
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Try:
    Code:
    template<typename T> typename List<T>::A List<T>::begin() const


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •