sorting - "invalid operands to binary expression" when using custom struct as index of map in C++ -


the code:

struct tag {     std::string left_tag, right_tag; }; 

when try use this->_tags[__tag] = true;, got error:

/applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:63:21: error: invalid operands binary expression ('const tag' , 'const tag')         {return __x < __y;}                 ~~~ ^ ~~~ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/map:1207:17: note: in instantiation of member function 'std::__1::less<tag>::operator()' requested here             if (__tree_.value_comp().key_comp()(__k, __nd->__value_.__cc.first))                 ^ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/map:1376:36: note: in instantiation of member function 'std::__1::map<tag, bool, std::__1::less<tag>, std::__1::allocator<std::__1::pair<const tag, bool> > >::__find_equal_key' requested here     __node_base_pointer& __child = __find_equal_key(__parent, __k);                                    ^ /users/xxx/githubworking/markuputils/syntax.h:69:20: note: in instantiation of member function 'std::__1::map<tag, bool, std::__1::less<tag>, std::__1::allocator<std::__1::pair<const tag, bool> > >::operator[]' requested here         this->_tags[__tag] = true;                    ^ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../include/c++/v1/utility:419:1: note: candidate template ignored: not match 'pair<type-parameter-0-0, type-parameter-0-1>' against 'const tag' operator< (const pair<_t1,_t2>& __x, const pair<_t1,_t2>& __y) ^ 1 error generated. 

i stunned series of long stl errors.

according deep error, caused use of const tag, there not seem in code.

you need define operator< example

struct tag {     std::string left_tag, right_tag;      bool operator< (const tag& rhs) const     {         return this->left_tag < rhs.left_tag ||                (this->left_tag == rhs.left_tag && this->right_tag < rhs.right_tag);     } }; 

this define less-than operator sort 2 tag instances. written in above example, sort preferring left_tag, right_tag.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -