[英]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>();
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 . 从注释来看,似乎您包括了
<string.h>
标头(这是标准C库标头),而不是<string>
标头, 后者是在其中定义std::string
类的地方 。
Second problem: 第二个问题:
You forgot to establish the inheritance relationship between your classes: 您忘记在类之间建立继承关系:
class AlarmDispatcher : public IDispatcher
// ^^^^^^^^
{
virtual void operator()()
{
}
};
For those who have this problem in the future: I resolved it by adding const at the end of the function definition, which I was using to make a class utilizable as a key in a c++ std map collection: 对于那些将来遇到此问题的人:我通过在函数定义的末尾添加const来解决了该问题,我使用该常量使一个类可用作c ++ std map集合中的键:
bool operator <(const ThisClass& otherInstance)
becomes: 变成:
bool operator <(const ThisClass& otherInstance) const
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.