Got the above error, when compiling something that looked like this:
std::map<std::string, myType> myMap;
myMap[“Test”] = myType();
Looked around on the net, didn’t find the answer, thought about it for a bit and then realised I was missing #include <string> which fixed the problem. The error was a bit cryptic. Look for yourself at this dump from the compiler output:
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(142) : while compiling class template member function ‘bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const’
1> with
1> [
1> _Ty=std::string
1> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\map(72) : see reference to class template instantiation ‘std::less<_Ty>’ being compiled
1> with
1> [
1> _Ty=std::string
1> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(26) : see reference to class template instantiation ‘std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,_Mfl>’ being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=std::string,
1> _Pr=std::less<std::string>,
1> _Alloc=std::allocator<std::pair<const std::string,std::string>>,
1> _Mfl=false
1> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(68) : see reference to class template instantiation ‘std::_Tree_nod<_Traits>’ 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> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(94) : see reference to class template instantiation ‘std::_Tree_ptr<_Traits>’ 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> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(112) : see reference to class template instantiation ‘std::_Tree_val<_Traits>’ 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> ]
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\map(82) : see reference to class template instantiation ‘std::_Tree<_Traits>’ 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> ]
1> c:\projects\flewse\source\x-planeflewse\VisualSettings.h(16) : see reference to class template instantiation ‘std::map<_Kty,_Ty>’ being compiled
1> with
1> [
1> _Kty=std::string,
1> _Ty=std::string
1> ]
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)’ : could not deduce template argument for ‘const std::_Tree<_Traits> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xtree(1372) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)’ : could not deduce template argument for ‘const std::vector<_Ty,_Alloc> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\vector(1276) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)’ : could not deduce template argument for ‘const std::reverse_iterator<_RanIt> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1880) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2784: ‘bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)’ : could not deduce template argument for ‘const std::pair<_Ty1,_Ty2> &’ from ‘const std::string’
1> C:\Program Files\Microsoft Visual Studio 8\VC\include\utility(76) : see declaration of ‘std::operator <‘
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\functional(143) : error C2676: binary ‘<‘ : ‘const std::string’ does not define this operator or a conversion to a type acceptable to the predefined operator
Thanks a lot, I was stuck with exactly the same problem.. and the same fix. I guess it was using another string definition somewhere.. THANK YOU!
Nice catch… i had exactly the same error which wasted my 2 hrs….
Thanks a lot
Thanks, good trick, fix my current problem (error messages are, like usually, very explicit…) !
WOOW, thank you so much! Very cryptic error message!
Thanks… With the same problem cannt #include work…until i changed it to #include…
Thanks… With the same problem cannt #include string.h work…until i changed it to #include string …
Thanks, it was exactly what I needed.
Thank you! Its am awesome catch !!
Thanks a lot for posting this.
Yes, this solved my headache too.
Thank you, guys! 😉
so much thanks
rock on! 🙂 saved my bacon
THANKS !! 🙂 Very grateful for your answer.
Thanks much! Now it’s number 1 on Google/Bing with “c2784 map” 🙂
Yes, this solved my headache too.
Thanks a lot 🙂
Aw yeah, thanks :3
lots thanks 🙂 you rock
Did you find why it solved the problem? Probably string would have overloaded the ‘<' operator?
This can happen when map which involves a user defined datatype as the key. To resolve this issue compiler is expecting a Overloaded version for operator “<" from the user who defined that datatype. When you write overloaded operator function user must keep in mind that he must not modify the left or right hand side operands of the call(By mentioning "const" keyword) .So versions protoytpes could be as follows:
1. bool operator < (const datatype& arg) const
{
//….body
}
2. bool operator < (datatype arg) const
{
//….body
}
If my explanation has helped you please reply me with Thank you…I want see to how many people it helps
Thank you for your explanation, I only just got a chance to check my e-mails so I was a bit slow in approving your comment. I put up this post 4 years ago but it still seems popular. Good to have a full explanation for what is going on.