I’m converting a project from Visual Studio 6 to Visual Studio 2010. The project heavily uses STL. There is a custom class that subclasses list to add more methods to it. Following is the code and compiler error that I’m getting.
#include <list>
namespace mySpace
{
template <class T>
class MyList : public std::list<T>
{
...
};
template <class T>
MyList<T>::reference MyList<T>::find(const_reference p_constreferenceItem) const
{
return std::find(this->begin(), this->end(), p_constreferenceItem);
}
} // namespace mySpace
Error:
Error 2 error C2143: syntax error : missing ';' before 'MySpace::MyList<T>::find' c:\myproject\mylist.h 300 1
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\myproject\mylist.h 300 1
Error 4 error C2888: 'MyList<T>::reference reference' : symbol cannot be defined within namespace 'MySpace' c:\myproject\mylist.h 300 1
Any ideas what is causing this error?
It seems like you’re just missing a
typenamein front ofMyList<T>::reference, since it’s a dependent type (one that depends on the type ofT) and the compiler doesn’t know thatMyList<T>::referenceis a type and assumes by default that it’s a variable name:You don’t need this for the
const_referencethough, as the compiler is inMyList<T>‘s scope as soon as he’s seen the::(that of the method name) and now knows thatconst_referencenames a type. In the same way you don’t need to usetypenamewhen you’re already inside theMyList<T>‘s scope, e.g. when declaring or inline-defining the method inside the class definition.I’m not sure why VC6 allowed this, though. But we all know that VC6 and templates aren’t that big friends, not to speak of the loose specification of C++ templates and their behaviour at that time.
disclaimer: Sorry to the language lawyers if the terminology used here is a bit informal.