简体   繁体   English

std ::查找一组shared_ptr

[英]std::find across a set of shared_ptr

I'm sure I'm doing something stupid here, but I can't see it. 我敢肯定我在这里做了些蠢事,但我看不到它。 Why doesn't the following compile? 以下为什么不编译?

#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've made a vector of shared_ptr to a class. 我已经为一个类创建了一个shared_ptr的向量。 The class has a Name() property. 该类具有Name()属性。 All I want to do is iterate the vector looking for an instance of the class with the given name. 我想要做的就是迭代向量,寻找具有给定名称的类的实例。 However, it doesn't compile :(. 但是,它没有编译:(。

Errors are: 错误是:

1>ClCompile: 1> test.cpp 1>c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\algorithm(41): error C2679: binary '==' : no operator found which takes a right-hand 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
1> ClCompile:1> test.cpp 1> c:\\ program files(x86)\\ microsoft visual studio 10.0 \\ vc \\ include \\ algorithm(41):错误C2679:二进制'==':找不到哪个操作符右转'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-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'::)' 1> with 1> [ 1> _Ty=StringClass 1>
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 '::)'1> with 1> [1> _Ty = StringClass 1>

] 1> c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\algorithm(74) : see reference to function template instantiation '_InIt std::_Find*, anonymous-namespace'::<lambda0>>(_InIt,_InIt,const anonymous-namespace':: &)' being compiled 1> with 1> [ 1> _InIt=std::tr1::shared_ptr *, 1> _Ty=StringClass 1> ] 1> c:\\svn\\trunk\\test\\test\\test.cpp(55) : see reference to function template instantiation '_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=
] 1> c:\\ program files(x86)\\ microsoft visual studio 10.0 \\ vc \\ include \\ algorithm(74):参见函数模板实例化'_InIt std :: _ Find *, anonymous-namespace'::<lambda0>>(_InIt,_InIt,const anonymous-namespace'::&)'正在编译1> 1> [1> _InIt = std :: tr1 :: shared_ptr *,1> _Ty = StringClass 1>] 1> c:\\ svn \\ trunk \\ test \\ test \\ test.cpp(55):参见函数模板实例化'_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.
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>生成代码... 1> 1>构建失败。
1> 1>Time Elapsed 00:00:00.87 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 1> 1>经过时间00:00:00.87 ==========构建:0成功,1失败,0最新,0跳过==========

You want to use find_if , not find . 你想使用find_if ,而不是find Find searches for a value, find_if takes a predicate. Find值的搜索, find_if采用谓词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM