Regrading STL Map checking for entry using find
I am following program
typedef std::map<std::string, CRTSLogManager*> FileNameToStor开发者_C百科ageClass;
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>'
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.
Shouldn't iter be declared as
FileNameToStorageClass::iterator iter;
?
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;
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.
精彩评论