简体   繁体   中英

C++, find_if with vector<unique_ptr> is not working properly

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;    
}

iteratorHelper is not const . You are calling it from a const function getAccInfo . This is illegal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM