jeudi 30 mars 2017

find_if with vector

I realize that similar questions has been asked before but i didn't find the answer to my question so I'm just gonna post it here.

iteratorHelper takes an accountNr as argument, searches a vector of unique_ptrs and returns a bool. The problems is in the second method.

The if-statement in getAccInfo(size_t pAccNr) is giving me problems. My IDE is complaining about the "iteratorHelper" saying that "The object has type qualifiers that are not compatible with the member function "Client::iteratorHelper". Object type is const Client" I'm not entirely sure what I'm missing here so if anyone could point me in the right direction?

    auto Client::iteratorHelper(size_t accountNr )    {
                return find_if(accounts.begin(), accounts.end(),
                    [&accountNr ](const unique_ptr<Account>& account)
                {return account->getAccountNr() == accountNr ; });

    unique_ptr<Account> const & Client::getAccInfo(size_t pAccNr) const    {
                if (iteratorHelper(pAccNr) != accounts.end())
                {
                    auto const& index = distance(accounts.begin(), iteratorHelper(pAccNr));
                    return accounts[index];
                }
                return nullptr;    
                }

Aucun commentaire:

Enregistrer un commentaire