tags:

views:

604

answers:

3

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.

+1  A: 

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.

anon
That occurs in vc6 right?
yesraaj
I think that is because of the C++ standard, so likely VC9 follows the standard and VC6 does not. The C++0x standard allows for the >> thing.
OregonGhost
unbelievable, if true.
kenny
It is true, but it is not the problem in this case - Tobi has the correct answer.
anon
+7  A: 

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
}
Tobi
Perfect! Thanks Tobi.Can you also explain why it was not required in VC6?
Bobby Alexander
Sorry, I don't know that exactly. I assume it was a bug in the VC6 compiler; I'm pretty sure VC6 didn't even come close to supporting everything related to templates (e.g. partial specialization.)
Tobi
It wasn't required by VC6 because VC6 is not anything near a standards compliant compiler. Templates and the STL were still not finalized when the compiler was released, so apart from the compiler being buggy and failing to follow spec, the spec it *attempts* to follow is different from the actual language standard.
jalf
+3  A: 

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.

Naveen
Does this compiler problem exist only with VC9?
Bobby Alexander
No it is not a 'bug'. That is how the standard is. It may compile in VC6, but VC6 is a horrible compiler when it comes to templates. It may be doing something totally different.. I wouldn't believe anything related to templates when it is compiled on VC6.
Naveen