2017-02-26 113 views
1

我提供了一个函数对象,find_name,以搜索匹配name特定的对象,但事实证明,我做错了什么:查找自定义对象

#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 

struct Item 
{ 
    Item() : name(), id(), value() { } 
    Item(std::string n, int i, double v) : name(n), id(i), value(v) { } 
    Item(const Item& src) : name(src.name), id(src.id), value(src.value) { } 

    Item& operator= (const Item& src) 
    { 
     Item temp(src); 
     swap(temp); 
     return *this; 
    } 

    void swap(Item& src) 
    { 
     std::swap(src.name, name); 
     std::swap(src.id, id); 
     std::swap(src.value, value); 
    } 

std::string name; 
int id; 
double value; 
}; 

//-------------------------------------------------------------------- 

void swap(Item& lhs, Item& rhs) { lhs.swap(rhs); } 

//-------------------------------------------------------------------- 

struct find_name 
{ 
    find_name(const std::string& t) : target(t) { } 
    bool operator() (const Item& src) const { return src.name == target; } 

    std::string target; 
}; 

//-------------------------------------------------------------------- 


void erase (std::vector<Item>& v, const std::string& name) 
{ 
    std::vector<Item>::iterator found = std::find(v.begin(), v.end(), find_name(name)); 
    //std::vector<Item>::iterator found = std::find(v.begin(), v.end(), [name] (const Item& src) { return src.name == name; }); 

    if (found != v.end()) 
    { 
     v.erase(found); 
    } 
    else 
    { 
     std::cout <<"\nName: "<< name <<" not found!\n"; 
    } 
} 

//-------------------------------------------------------------------- 


int main() 
{ 
    std::vector<Item> items; 
    items.push_back(Item("horse shoe", 99, 12.34)); 

    std::string name("horse shoe"); 
    erase(items, name); 

    return 0; 
} 

是什么在find功能的问题?


按照要求:错误消息:

In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Value = const find_name]’: 
/usr/include/c++/6/bits/stl_algo.h:120:14: required from ‘_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const find_name>]’ 
/usr/include/c++/6/bits/stl_algo.h:161:23: required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const find_name>]’ 
/usr/include/c++/6/bits/stl_algo.h:3792:28: required from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Item*, std::vector<Item> >; _Tp = find_name]’ 
prog.cpp:77:83: required from here 
/usr/include/c++/6/bits/predefined_ops.h:199:17: error: no match for ‘operator==’ (operand types are ‘Item’ and ‘const find_name’) 
    { return *__it == _M_value; } 
      ~~~~~~^~~~~~~~~~~ 
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/stl_iterator.h:856:5: note: candidate: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&) 
    operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, 
    ^~~~~~~~ 
/usr/include/c++/6/bits/stl_iterator.h:856:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’ 
    { return *__it == _M_value; } 
      ~~~~~~^~~~~~~~~~~ 
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/stl_iterator.h:863:5: note: candidate: template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) 
    operator==(const __normal_iterator<_Iterator, _Container>& __lhs, 
    ^~~~~~~~ 
/usr/include/c++/6/bits/stl_iterator.h:863:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::__normal_iterator<_Iterator, _Container>’ 
    { return *__it == _M_value; } 
      ~~~~~~^~~~~~~~~~~ 
In file included from /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0, 
       from /usr/include/c++/6/bits/allocator.h:46, 
       from /usr/include/c++/6/string:41, 
       from /usr/include/c++/6/bits/locale_classes.h:40, 
       from /usr/include/c++/6/bits/ios_base.h:41, 
       from /usr/include/c++/6/ios:42, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/ext/new_allocator.h:139:5: note: candidate: template<class _Tp> bool __gnu_cxx::operator==(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&) 
    operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&) 
    ^~~~~~~~ 
/usr/include/c++/6/ext/new_allocator.h:139:5: note: template argument deduction/substitution failed: 
In file included from /usr/include/c++/6/bits/stl_algobase.h:71:0, 
       from /usr/include/c++/6/bits/char_traits.h:39, 
       from /usr/include/c++/6/ios:40, 
       from /usr/include/c++/6/ostream:38, 
       from /usr/include/c++/6/iostream:39, 
       from prog.cpp:1: 
/usr/include/c++/6/bits/predefined_ops.h:199:17: note: ‘Item’ is not derived from ‘const __gnu_cxx::new_allocator<_Tp>’ 
    { return *__it == _M_value; } 
      ~~~~~~^~~~~~~~~~~ 

Live sample here

+1

编译和运行代码时会发生什么?你有任何错误?如果是这样,他们是什么?如果不是,输出是什么,它与你想要的有什么不同? –

+0

@ Code-Apprentice我在最后提供了一个链接。它工作吗? – Ziezi

+1

请直接在这里显示输出。 –

回答