2013-05-17 4 views
-1

Как я могу решить следующую ошибку компилятора:бинарная '<': нет оператора найдено для отображения <станд :: строка, shared_ptr <Foo>>

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

ответ

5

Первая проблема:

Вы должны включать явно все необходимые заголовки, а не полагаться на них, косвенно включаться через inclusio п других заголовков:

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

Судя по комментариям, кажется, вы включили <string.h> заголовок (который является заголовок стандартной библиотеки C), а не заголовок <string>, which is where the std::string class is defined.

Вторая проблема:

Вы забыли установить отношения наследования между вашими классами:

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

Почему не std :: строка определена в string.h? – Baz

+2

@Baz: Это то, что требует стандарт C++. 'std :: string' определен в заголовке' ', а заголовок' string.h' (такой же, как '' в C++ 11) является заголовком библиотеки строк стандартной строки –

0

Для тех, кто имеет эту проблему в будущем: я решил, добавив сопзЬ в конце определения функции, которое я использовал для создания класса, используемого в качестве ключа в коллекции карт C++ std:

bool operator <(const ThisClass& otherInstance) 

будет:

bool operator <(const ThisClass& otherInstance) const 
+0

http://stackoverflow.com/ вопросы/726456/C-шаблон класс ошибки с-оператор – Andrew

Смежные вопросы