【问题标题】:binary '<' : no operator found for map<std::string, shared_ptr<Foo>>二进制 '<' : 找不到 map<std::string, shared_ptr<Foo>> 的运算符
【发布时间】:2013-05-17 11:03:37
【问题描述】:

如何解决以下编译器错误:

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
          ]

以下代码:

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>();

【问题讨论】:

    标签: c++


    【解决方案1】:

    第一个问题:

    您应该明确包含所有必要的标头,而不是依赖于通过包含其他标头来间接包含它们:

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

    从 cmets 看来,您似乎包含了 &lt;string.h&gt; 标头(这是标准 C 库标头)而不是 &lt;string&gt; 标头 which is where the std::string class is defined

    第二个问题:

    你忘了在你的类之间建立继承关系:

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

    【讨论】:

    • 为什么在 string.h 中没有定义 std::string?
    • @Baz:这就是 C++ 标准的要求。 std::string 定义在 &lt;string&gt; 标头中,而 string.h 标头(与 C++11 中的 &lt;cstring&gt; 相同)是标准 C 字符串库标头
    【解决方案2】:

    对于将来遇到此问题的人:我通过在函数定义的末尾添加 const 来解决它,我用它来使类可用作 c++ std map 集合中的键:

    bool operator <(const ThisClass& otherInstance)
    

    变成:

    bool operator <(const ThisClass& otherInstance) const
    

    【讨论】:

      猜你喜欢
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多