Don't use .insert.
Instead do map[first] = second;
I'm trying to use std::map but its not working like all of the examples on the internet show. I have no idea what these error messages mean.
In my class definition:
std::map<std::string, std::string> m_optionlist;
std::map<std::string, std::string>::const_iterator m_optioniter;
The problem code:
while( m_optioniter = m_optionlist.begin(); m_optionlist != m_optionlist.end(); ++m_optionlist ) // Line 60 *****
{
config.append( "<option name='" );
config.append( m_optioniter->first );
config.append( "' value='" );
config.append( m_optioniter->second );
config.append( "' />" );
}
I get about 102 errors like this:
1>c:\projects\brokensilence\bsclient\trunk\src\brokensilence\config.cpp(60) : error C2143: syntax error : missing ')' before ';'
1>c:\projects\brokensilence\bsclient\trunk\src\brokensilence\config.cpp(60) : error C2451: conditional expression of type 'std::_Tree<_Traits>::const_iterator' is illegal
1> with
1> [
1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
[Edited by - Stormtrooper on August 3, 2007 10:27:03 PM]
Cancel
Save
m_optionlist != m_optionlist.end();
while(x; y; z)
:S
Also, it's generally a bad idea to keep iterators as class members - and especially here: don't use a class member for wat could be a local variable.
Also, assuming 'config' is a std::string, why not use the more natural syntax of operator overloads? :)
typedef std::map<std::string, std::string> optionlist;optionlist m_optionlist;for (optionlist::const_iterator it = m_optionlist.begin(); it != m_optionlist.end(); ++it) { config += "<option name='" + it->first + "' value='" + it->second + "' />";}
Cancel
Save
Ok thanks. But I still get an error on a file that is not apart of my code, but std I think. 1>c:\program files\microsoft visual studio 8\vc\include\xtree(743) : error C2664: 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert(const std::pair<const _Kty,_Ty> &)' : cannot convert parameter 1 from 'const char' to 'const std::pair<_Ty1,_Ty2> &' 1> with 1> [ 1> _Ty1=std::_Tree<std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>>::iterator, 1> _Ty2=bool, 1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>, 1> _Kty=std::string, 1> _Ty=std::string 1> ] 1> and 1> [ 1> _Ty1=const std::string, 1> _Ty2=std::string 1> ] 1> Reason: cannot convert from 'const char' to 'const std::pair<_Ty1,_Ty2>' 1> with 1> [ 1> _Ty1=const std::string, 1> _Ty2=std::string 1> ] 1> No constructor could take the source type, or constructor overload resolution was ambiguous 1> c:\projects\brokensilence\bsclient\trunk\src\brokensilence\utilities\config.cpp(43) : see reference to function template instantiation 'void std::_Tree<_Traits>::insert<const char*>(_Iter,_Iter)' being compiled 1> with 1> [ 1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>, 1> _Iter=const char * 1> ] 1>c:\program files\microsoft visual studio 8\vc\include\xtree(742) : error C2675: unary '++' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator 1> c:\projects\brokensilence\bsclient\trunk\src\brokensilence\utilities\config.cpp(87) : see reference to function template instantiation 'void std::_Tree<_Traits>::insert<std::string>(_Iter,_Iter)' being compiled 1> with 1> [ 1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>, 1> _Iter=std::string 1> ] 1>c:\program files\microsoft visual studio 8\vc\include\xtree(743) : error C2100: illegal indirection 1>c:\program files\microsoft visual studio 8\vc\include\xtree(743) : error C2664: 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert(const std::pair<const _Kty,_Ty> &)' : cannot convert parameter 1 from 'std::string' to 'const std::pair<_Ty1,_Ty2> &' 1> with 1> [ 1> _Ty1=std::_Tree<std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>>::iterator, 1> _Ty2=bool, 1> _Traits=std::_Tmap_traits<std::string,std::string,std::less<std::string>,std::allocator<std::pair<const std::string,std::string>>,false>, 1> _Kty=std::string, 1> _Ty=std::string 1> ] 1> and 1> [ 1> _Ty1=const std::string, 1> _Ty2=std::string 1> ] 1> Reason: cannot convert from 'std::string' to 'const std::pair<_Ty1,_Ty2>' 1> with 1> [ 1> _Ty1=const std::string, 1> _Ty2=std::string 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Cancel
Save
Quote: Original post by Stormtrooper Ok thanks. But I still get an error on a file that is not apart of my code, but std I think.No, they wouldn't ship your compiler if its standard library implementation could be at fault for compiler errors.
I'm not psychic; show your updated code. Probably more context is required.
Cancel
Save
Quote: Original post by Zahlman I'm not psychic; show your updated code. Probably more context is required.Judging from his errors, I would suspect that he's trying to:
std::map<std::string,std::string> the_map;the_map.insert("string literal");
Cancel
Save
Quote: Original post by ToohrVyk Quote: Original post by Zahlman I'm not psychic; show your updated code. Probably more context is required. Judging from his errors, I would suspect that he's trying to:std::map<std::string,std::string> the_map;the_map.insert("string literal");
Yea basically.
typedef std::map<std::string, std::string> OptionList;OptionList m_optionlist;// save config functionfor( iter = m_optionlist.begin(); iter != m_optionlist.end(); ++iter ){ config.append( "<option name='" ); config.append( iter->first ); config.append( "' value='" ); config.append( iter->second ); config.append( "' />" );}// add optionm_optionlist.insert( name, value );// get option for( iter = m_optionlist.begin(); iter != m_optionlist.end(); ++iter ) { if( iter->first == name ) return iter->second; }
Cancel
Save
Quote: Original post by Stormtrooper // add optionm_optionlist.insert( name, value );
Is 'name' or 'value' or are they both of type "char *"? Because if they are, you should change that to:
m_optionlist.insert( std::string(name) , std::string(value) );
Quote: // get option for( iter = m_optionlist.begin(); iter != m_optionlist.end(); ++iter ) { if( iter->first == name ) return iter->second; }Why do THAT when you have a perfectly good find function?
if(m_optionlist.find(name) != m_optionlist.end()) return m_optionlist[name];
Cancel
Save
Quote: // get option for( iter = m_optionlist.begin(); iter != m_optionlist.end(); ++iter ) { if( iter->first == name ) return iter->second; }Way to completely miss out on the whole reason for using std::map in the first place, efficient find operations [smile]
Cancel
Save