Std :: find via shared_ptr set

I'm sure I'm doing something stupid here, but I don't see it. Why the next compilation?

#include <algorithm> #include <memory> #include <vector> #include <string> // A class to play with. Encapsulates a name. class StringClass { public: StringClass(std::string const & name) : MyName(name) { } std::string const & Name() const { return MyName; } private: std::string MyName; }; // The set of instances of "StringClass". std::vector<std::shared_ptr<StringClass>> MyInstances; // Function returns "true" if a class with the given name exists in the collection. bool Exists(std::string const & name) { auto i = std::find(MyInstances.begin(), MyInstances.end(), [&](std::shared_ptr<StringClass> const & instance) { return instance->Name() == name; }); return i != MyInstances.end(); } 

I made a shared_ptr vector for the class. The class has a Name () property. All I want to do is iterate over the vector looking for an instance of the class with the given name. However, it does not compile: (.

Errors:

1> ClCompile: 1> test.cpp 1> c: \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ algorithm (41): error C2679: binary '==': no ​​found statement that accepts the right operand of type 'const anonymous-namespace'::<lambda0>' (or there is no acceptable conversion) 1> could be 'built-in C++ operator==(std::_Bool_type, std::_Bool_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)' 1> while trying to match the argument list '(std::tr1::shared_ptr<_Ty>, const
anonymous-namespace'::<lambda0>' (or there is no acceptable conversion) 1> could be 'built-in C++ operator==(std::_Bool_type, std::_Bool_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)' 1> while trying to match the argument list '(std::tr1::shared_ptr<_Ty>, const
anonymous-namespace'::<lambda0>' (or there is no acceptable conversion) 1> could be 'built-in C++ operator==(std::_Bool_type, std::_Bool_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or 'bool std::operator ==(const std::error_condition &,const std::error_code &)' 1> while trying to match the argument list '(std::tr1::shared_ptr<_Ty>, const
anonymous names " : :) 1> with 1> [1> _Ty = StringClass 1>
] 1> c: \ program files (x86) \ microsoft visual studio 10.0 \ vc \ include \ algorithm (74): see Link to the function template instance '_InIt std :: _ Find *, anonymous-namespace'::<lambda0>>(_InIt,_InIt,const anonymous-namespace ':: &)' compiled 1> with 1> [1> _InIt = std :: tr1 :: shared_ptr *, 1> _Ty = StringClass 1>] 1> c: \ svn \ trunk \ test \ test \ test.cpp (55): see the link to the function to create the template '_InIt std :: find, anonymous-namespace'::<lambda0>>(_InIt,_InIt,const _Ty &)' being compiled 1> with 1> [ 1> _InIt=std::_Vector_iterator<std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>>, 1>
_Myvec=std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>, 1> _Ty=
anonymous-namespace'::<lambda0>>(_InIt,_InIt,const _Ty &)' being compiled 1> with 1> [ 1> _InIt=std::_Vector_iterator<std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>>, 1>
_Myvec=std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>, 1> _Ty=
anonymous-namespace'::<lambda0>>(_InIt,_InIt,const _Ty &)' being compiled 1> with 1> [ 1> _InIt=std::_Vector_iterator<std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>>, 1>
_Myvec=std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>, 1> _Ty=
anonymous-namespace ':: 1>] 1 > stdafx.cpp 1> Generating code ... 1> 1> Build FAILED. 1> 1> Elapsed time 00: 00: 00.87 =========== Build: 0 succeeded, 1 failed, 0 updated, 0 skipped ==========

+4
source share
1 answer

You want to use find_if , not find . find searches for a value, find_if accepts a predicate.

+8
source

All Articles