Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

c++ - binary '<' : no operator found for map<std::string, shared_ptr<Foo>>

How can I resolve the following compiler error:

xstddef(180): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const std::string' (or there is no acceptable conversion)
          tuple(572): could be 'bool std::operator <(const std::tuple<> &,const std::tuple<> &)'
          while trying to match the argument list '(const std::string, const std::string)'
          xstddef(179) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const'
          with
          [
              _Ty=std::string
          ]
          map(177) : see reference to function template instantiation 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' being compiled
          with
          [
              _Ty=std::string
          ]
          type_traits(743) : see reference to class template instantiation 'std::less<_Ty>' being compiled
          with
          [
              _Ty=std::string
          ]
          xtree(1028) : see reference to class template instantiation 'std::is_empty<_Ty>' being compiled
          with
          [
              _Ty=std::less<std::string>
          ]
          map(67) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled
          with
          [
              _Traits=std::_Tmap_traits<std::string,IDispatcherPtr,std::less<std::string>,std::allocator<std::pair<const std::string,IDispatcherPtr>>,false>
          ]
          main.cpp(506) : see reference to class template instantiation 'std::map<_Kty,_Ty>' being compiled
          with
          [
              _Kty=std::string,
              _Ty=IDispatcherPtr
          ]

for the following code:

class IDispatcher
{
    virtual void operator()() = 0;
};

class AlarmDispatcher: public IDispatcher
{
    virtual void operator()()
    {
    }
};

typedef boost::shared_ptr<IDispatcher> IDispatcherPtr;

int main()
{
    std::map<std::string, IDispatcherPtr> dispatchers;
    dispatchers["alarm"] = boost::make_shared<AlarmDispatcher>();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First problem:

You should include explicitly all the necessary headers, and not rely on them being indirectly included through the inclusion of other headers:

#include <string> // <== IN PARTICULAR THIS ONE
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

Judging from the comments, it seems you included the <string.h> header (which is a Standard C library header) rather than the <string> header, which is where the std::string class is defined.

Second problem:

You forgot to establish the inheritance relationship between your classes:

class AlarmDispatcher : public IDispatcher
//                    ^^^^^^^^
{
    virtual void operator()()
    {
    }
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...