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 =-
[Class]
template<typename T> class List
{
public:
    class A
    {
    };

    A begin() const;
};
[/Class]


-= IMPLEMENTATION FILE =-
[Class]
template<typename T> A List<T>::begin() const
{
    return A();
}
[Class]

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,

Because templates have to go in header files.

When you say something like List<int>, it needs to know what the template expansion of that template actually looks like to be able to
- declare instances of the class
- call class member functions with the right params
- etc

It also needs to generate the code for that type instance as well.

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

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,

When the compiler complains about dependent names, it means you need to use the typename keyword to fix an ambiguity:

template<typename T> class List
{
public:
	class A
	{
	};

	A begin() const;
};

template<typename T>
typename List<T>::A List<T>::begin() const
{
	return A();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.