fixed: error C2784 from compiling adding an entry to a std::map

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

 

This entry was posted in Uncategorized. Bookmark the permalink.

21 Responses to fixed: error C2784 from compiling adding an entry to a std::map

  1. Felix says:

    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!

  2. Gaurav says:

    Nice catch… i had exactly the same error which wasted my 2 hrs….
    Thanks a lot

  3. Flo says:

    Thanks, good trick, fix my current problem (error messages are, like usually, very explicit…) !

  4. Dan says:

    WOOW, thank you so much! Very cryptic error message!

  5. Hongqing says:

    Thanks… With the same problem cannt #include work…until i changed it to #include…

  6. Hongqing says:

    Thanks… With the same problem cannt #include string.h work…until i changed it to #include string …

  7. Stephane says:

    Thanks, it was exactly what I needed.

  8. Ashwin Kumar says:

    Thank you! Its am awesome catch !!

  9. Rayniery says:

    Thanks a lot for posting this.

  10. Nebullus says:

    Yes, this solved my headache too.
    Thank you, guys! 😉

  11. phamthang says:

    so much thanks

  12. peter says:

    rock on! 🙂 saved my bacon

  13. Alastair says:

    THANKS !! 🙂 Very grateful for your answer.

  14. Dev says:

    Thanks much! Now it’s number 1 on Google/Bing with “c2784 map” 🙂

  15. koby meir says:

    Yes, this solved my headache too.

    Thanks a lot 🙂

  16. Meteotrix says:

    Aw yeah, thanks :3

  17. nwish says:

    lots thanks 🙂 you rock

  18. Did you find why it solved the problem? Probably string would have overloaded the ‘<' operator?

  19. Mr.Venkatesh Pai says:

    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
    }

    • Mr.Venkatesh Pai says:

      If my explanation has helped you please reply me with Thank you…I want see to how many people it helps

    • johnstewien says:

      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.

Leave a comment