二进制'' :未找到针对地图的操作符< std :: string,shared_ptr< Foo>> [英] binary '<' : no operator found for map<std::string, shared_ptr<Foo>>
问题描述
如何解决以下编译器错误:
xstddef(180):error C2678:binary'<':没有操作符发现,它接受类型为const std :: string'(或没有可接受的转换)的左手操作数
tuple(572):可以是'bool std :: operator<(const std: :const std :: string,const std :: string)'
(const std :: string,const std :: string) xstddef(179):编译类模板成员函数'bool std :: less< _Ty> :: operator()(const _Ty&,const _Ty&)const'
with
[
_Ty = std :: string
]
map(177):参见函数模板实例化的参考'bool std :: less< _Ty> :: operator()(const _Ty&,const _Ty &)const'被编译
with
[
_Ty = std :: string
]
type_traits(743):参见类模板实例化'std: :less <_Ty>'正在编译
with
[
_Ty = std :: string
]
xtree(1028):参见类模板实例化:: is_empty< _Ty>'编译
with
[
_Ty = std :: less< std :: string>
]
map(67):参见类模板实例化'std :: _ Tree< _Traits>'被编译
with
[
_Traits = std :: _Tmap_traits< std :: string,IDispatcherPtr,std :: less< std :: string>,std :: allocator< std :: pair< const std :: string,IDispatcherPtr>,false>
]
main.cpp(506):参见类模板实例化的参考'std :: map <_Kty,_Ty>'正在编译
with
[
_Kty = std :: string,
_Ty = IDispatcherPtr
]
代码:
class IDispatcher
{
virtual void operator()
};
class AlarmDispatcher:public IDispatcher
{
virtual void operator()
{
}
};
typedef boost :: shared_ptr< IDispatcher> IDispatcherPtr;
int main()
{
std :: map< std :: string,IDispatcherPtr>调度员
dispatchers [alarm] = boost :: make_shared< AlarmDispatcher>();
第一个问题: / p>
您应该明确包含所有必需的标题,而不是依赖于通过包含其他标题间接包含的:
#include< string> //< == INTICTICULAR THIS ONE
#include< map>
#include< boost / shared_ptr.hpp>
#include< boost / make_shared.hpp>
从评论判断,你似乎包括< string.h> ;
头部(这是一个标准C库头部),而不是< string>
头部,其中定义了 std :: string
类。
第二个问题:
您忘记建立类之间的继承关系:
class AlarmDispatcher:public IDispatcher
// ^^^^^^^^
{
virtual void operator()
{
}
};
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>();
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()()
{
}
};
这篇关于二进制'' :未找到针对地图的操作符< std :: string,shared_ptr< Foo>>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!