I have a problem passing a map<...>::iterator object to a function as a const_iterator & on GCC:
class MyClass {
};
bool MyClass::_GetInstList(map<string,InstList>::const_iterator & it, const string & sMod)
{
cout<<"Matched\n";
it = tInstancesData.find(sMod);
if( it == tInstancesData.end() ) {
cout<<"\""<<sMod<<"\" is NOT a module\n";
return false;
}
return true;
}
bool SomeFunction()
{
map<string,InstList>::iterator it;
if( ! _GetInstList(it, SomeString) ) return false;
it->second.Add(...); // Modifying element pointed by "it"
}
My probelm is that on Visual Studio 2010 the code above works perfectly fine, but on GCC 4.1.2 I get an error saying there is no matching function to the function call, for _GetInstList(it, SomeString). The issue seems to be converting iterator to const_iterator &.
I have to take it by reference because "it" gets changed inside _GetInstList() and the caller function checks it. (The "it" pointer is changed not a pointed element).
Also, the "it" in SomeFunction() cannot be const because it changes an element.
How can I resolve this?
EDIT: For those who suggest that the problem is the conversion from iterator to const_iterator: The code compiles fine if the function prototype is changed to take const_iterator NOT as a reference, the problem is the const &.
_GetInstList()need aconst_iterator? - Adam27Xiteratoris convertible toconst_iterator. It doesn't say that aniteratoris aconst_iterator, so the same does not apply for a reference to it. It doesn't inherit from it's const counterpart, so it is a conversion which needs a cast. - leemes_GetInstListto accept no iterator argument and simply return a pair of abool(trueif found) and a non-const iterator (valid if found). Wouldn't that make more sense? - Andy Prowl