Error when using vector .find() member function

Hi all,

So I have the following piece of code (truncated for clarity's sake):

cout<<"Please enter the name of the app you wish to find";
cin>>AppSearchName;

appSearchIter=find(appVector.begin(), appVector.end(), AppSearchName);

if (appSearchIter!=appVector.end())
cout<<"App found!";
else
cout<<"Could not find app. Please try again";


AppSearchName is the string variable I'm searching for. appVector is a vector which holds instances of a class (called app). appSearchIter is the iterator for appVector. When I compile the code I get the following error message, which means absolutely nothing to me:

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 std::string' (or there is no acceptable conversion)
1> could be 'built-in C++ operator==(App *, App *)'
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 '(App *, const std::string)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find<App**,_Ty>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=App **,
1> _Ty=std::string
1> ]
1> c:\users\\\\\\\creativeappstore.cpp(118) : see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<_Myvec>,std::string>(_InIt,_InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=std::_Vector_iterator<std::_Vector_val<App *,std::allocator<App *>>>,
1> _Myvec=std::_Vector_val<App *,std::allocator<App *>>,
1> _Ty=std::string
1> ]


I've had a look at the files indicated in the directory paths, but they make no sense to me either. Any help would be thoroughly appreciated!
What's the type of appVector? std::vector<App*> ? The last argument of find is what you compare elements to. There is no default way to compare App* to std::string. What you need to do is write your own comparison function.
Hi hamsterman,

That's right it's vector<App*>. Sorry, what do you mean when you say comparison function? Do you mean it needs to compare the search term 'AppSearchName' to the various elements stored within the app classes in the <App*> vector?
My bad. find doesn't accept a comparison function. You need to use find_if. http://cplusplus.com/reference/algorithm/find_if/ - see the example. You'll have to write a function (or a class) that, when passed an App* (overload operator () in the class case) returns true if it's name is equal to AppSearchName.
Topic archived. No new replies allowed.