Venkata
Venkata

Reputation: 513

Regrading STL Map checking for entry using find

I am following program

typedef std::map<std::string, CRTSLogManager*> FileNameToStorageClass;
FileNameToStorageClass  m_mapFileNameToLogStorage;
map<string, void*>::iterator iter;
iter =m_mapFileNameToLogStorage.find(cFileName);

if(iter == m_mapFileNameToLogStorage.end())
{
   typedef std::pair<std::string, CRTSLogManager*> FileNameToStorageClassPair;
   string strFilename = "MyFile";
   CRTSLogManager *pLogManager = new CRTSLogManager();
   m_mapFileNameToLogStorage.insert(
      FileNameToStorageClassPair(strFilename, pLogManager));
}

I am getting following error while compiling which is related to == check in if condition.

no match for 'operator==' in 'iter == ((CRTSLogManagerReal*)this)->CRTSLogManagerReal::m_mapFileNameToLogStorage.std::map, std::allocator >, CRTSLogManager*, std::less, std::allocator > >, std::allocator, std::allocator >, CRTSLogManager*> >::.std::_Tree<_Traits>::end with _Traits = std::_Tmap_traits, std::allocator >, CRTSLogManager*, std::less, std::allocator > >, std::allocator, std::allocator >, CRTSLogManager*> >, false>'

Upvotes: 1

Views: 240

Answers (4)

Steve M
Steve M

Reputation: 8526

std::map<std::string, CRTSLogManager*> and map<string, void*> are different types and do not have the same iterator type, so you cannot compare two instances of those iterators. If you're going to use typedefs, you should use them consistently:

FileNameToStorageClass::iterator iter;

Upvotes: 2

jbremnant
jbremnant

Reputation: 894

Perhaps you are missing a typename arguments for templatized class like std::map?

What is the class or data type for your key/value pair? Seems like you want std::string for your key and pointer to CRTSLogManager object as the value.

In that case, your typedef alias FileNameToStorageClass should probably be:

typedef std::map<std::string, CRTSLogManager*> FileNameToStorageClass;

However, the code you posted does not seem to be a working program at all. For example, this line:

string strFilename = "MyFile"

won't work unless you defined your own class "string" or have std namespace loaded. I am assuming you are using std::string.

Upvotes: 0

a1ex07
a1ex07

Reputation: 37382

Shouldn't iter be declared as

FileNameToStorageClass::iterator iter;

?

Upvotes: 4

user124493
user124493

Reputation:

You are declaring an iterator for a

std::map <string, void *>

but you are trying to compare against an iterator for a

std::map< std::string, CRTSLogManager*>

They are not compatible.

Upvotes: 5

Related Questions