0
votes

I am using the map.find(key) and map.end() functions in an if statement:

if( p_Repos->returnTypeMap().find(tc[0]) != p_Repos->returnTypeMap().end() ) 

But it doesn't work, and I get a Microsoft Visual C++ Runtime Library error that tells me "Expression: list iterators incompatible". tc[0] is just a string, and the key position in my map is a string.

But, they should be compatible, right?

Any help is greatly appreciated.

Thanks, Tom

Edit: Based on an answer found here: Finding value in unordered_map, I'm lead to believe this should def work.

Second Edit:
Here is the returnTypeMap() function:

std::unordered_map <std::string, std::pair<std::string, std::string>> returnTypeMap()
{
      return typeTable;
}

Here is the definition of my map:

std::unordered_map <std::string, std::pair<std::string, std::string>> typeTable;
1
Does returnTypeMap() return by value? If so, each iterator is pointing in to a completely different map.Mankarse
Mankarse, I am not sure what you mean. Should I set a local map = to the map I am storing in my p_Repos object?traggatmot
Maybe you should use if( p_Repos->returnTypeMap().count(tc[0]) > 0 ) instead.leewz

1 Answers

5
votes

You are returning the map by value, so each call evaluates to a completely different map. Iterators in to different containers are not compatible, and attempting to compare them has undefined behaviour.

Try changing your code to return by const reference:

std::unordered_map<std::string, std::pair<std::string, std::string>> const& 
returnTypeMap() const
{
    return typeTable;
}

or make a local copy of the map and call find and end on the single local copy:

auto typeTable{p_Repos->returnTypeMap()};
if (typeTable.find(tc[0]) != typeTable.end()) {
    //...
}