STL中没有匹配的呼叫

时间:2020-07-27 14:25:37

标签: c++ stl

我将指向对象的指针存储在vector(STL)中。我正在根据帐号执行一些操作,我正在使用lambda函数来解决它。 但是我遇到了一些错误。 如何解决????

class BankingSystem {
  vector <Account *> vobject;
 public :
  BankingSystem();
  void Create_new_Account();
  void Account_info();
  void Get_All_Account_info();
  void Withdraw();
};

void BankingSystem :: Account_info()
{
    long int ACC_NUMBER;
    cout << "Enter AccountNumber : " <<endl;
    cin >> ACC_NUMBER;

    vector<Account*> :: iterator mAccount = std::find_if(vobject.begin(), vobject.end(),
                                 [&ACC_NUMBER](const Account& a) {
                                     // acc_number is maybe called something else in
                                     // your Account class.
                                     return a.Acc_holder.Account_number == ACC_NUMBER;
                                 });
    if(mAccount != vobject.end())
    {                                
        cout << (*mAccount)->Acc_holder.Account_number << endl;
        cout << (*mAccount)->Acc_holder.aadharNo << endl;
        vobject.erase(mAccount); // not "delete mAccount;"
    }
}

这是一个错误。我没有得到确切的错误是什么?请详细说明。

In file included from /usr/include/c++/7/bits/stl_algobase.h:71:0,
                     from /usr/include/c++/7/bits/char_traits.h:39,
                     from /usr/include/c++/7/ios:40,
                     from /usr/include/c++/7/ostream:38,
                     from /usr/include/c++/7/iostream:39,
                     from /home/pankaj/BANK/./include/stdheader.h:4,
                     from /home/pankaj/BANK/src/bankingSystem.cpp:1:
    /usr/include/c++/7/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<account**, std::vector<account*> >; _Predicate = BankingSystem::Account_info()::<lambda(Account&)>]’:
    /usr/include/c++/7/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<account**, std::vector<account*> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<BankingSystem::Account_info()::<lambda(Account&)> >]’
    /usr/include/c++/7/bits/stl_algo.h:161:23:   required from ‘_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<account**, std::vector<account*> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<BankingSystem::Account_info()::<lambda(Account&)> >]’
    /usr/include/c++/7/bits/stl_algo.h:3932:28:   required from ‘_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<account**, std::vector<account*> >; _Predicate = BankingSystem::Account_info()::<lambda(Account&)>]’
    /home/pankaj/BANK/src/bankingSystem.cpp:40:35:   required from here
    /usr/include/c++/7/bits/predefined_ops.h:283:11: error: no match for call to ‘(BankingSystem::Account_info()::<lambda(Account&)>) (account*&)’
      { return bool(_M_pred(*__it)); }
               ^~~~~~~~~~~~~~~~~~~~
    /home/pankaj/BANK/src/bankingSystem.cpp:36:58: note: candidate: BankingSystem::Account_info()::<lambda(Account&)>
                                      [&ACC_NUMBER](Account& a) {
                                                              ^
    /home/pankaj/BANK/src/bankingSystem.cpp:36:58: note:   no known conversion for argument 1 from ‘account*’ to ‘Account& {aka account&}’

1 个答案:

答案 0 :(得分:3)

查看代码的这一行:

vector<Account*> :: iterator mAccount = std::find_if(vobject.begin(), vobject.end(),
                             [&ACC_NUMBER](const Account& a) {

您的lambda应该接受Account *,而不是Account,因为您要遍历std::vector<Account *>

相关问题