提问者:小点点

STL中的呼叫不匹配


我正在向量中存储指向对象的指针(STL)。 并且我正在执行一些基于帐号的操作,我正在使用lambda函数来解决它。 但我犯了点错误。 如何修复??? //banksystem.cpp

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个答案

匿名用户

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

假设您有一个std::vector; vobject,您的lambda应该接受帐户*,而不是帐户