missing ; before identifier while compiling VC6 code in VC9
Question
The following code compiles fine in VC6 but when I compile the same project in VS2008 it gives the following error error C2146: syntax error : missing ';' before identifier 'm_pItr'
template <class pKey, class Data, class pCompare,
class hKey = int, class hCompare = less<hKey>,
class sKey = int, class sCompare = less<sKey>,
class tKey = int, class tCompare = less<tKey>,
class cKey = int, class cCompare = less<cKey>>
class GCache
{
private:
typedef map<pKey, Data, pCompare> PRIMARY_MAP;
PRIMARY_MAP pMap;
PRIMARY_MAP::iterator m_pItr; //error here
//Code truncated
}
Any ideas of what is wrong here? Someone with experience in migrating C++ code from VC6 to VC2005/2008 might be able to help.
Solution
You may need to insert 'typename', to tell the compiler PRIMARY_MAP::iterator is, in all cases, a type.
e.g.
class GCache
{
private:
typedef map<pKey, Data, pCompare> PRIMARY_MAP;
PRIMARY_MAP pMap;
typename PRIMARY_MAP::iterator m_pItr;
//Code truncated
}
OTHER TIPS
It should be typename PRIMARY_MAP::iterator m_pItr;
. Otherwise compiler thinks that PRIMARY_MAP::iterator is a static object and will not be able to recognize it as a type. So you have to give an hint to the compiler indicating that it is a type and not a static object.
You may be falling victim to a common template problem:
class cKey = int, class cCompare = less<cKey>>
should be:
class cKey = int, class cCompare = less<cKey> >
Note the space between the llast two angle brackets.