I've implemented a list and iterator templates, the find method is supposed to receive a functor so I declared and implemented one but I keep getting the error that there's no such an object! " no matching function for call to object of type const findBond "
here's the implementation of the find method:
template <class T>
template <class Predicate>
Iterator<T> List<T> :: find(const Predicate &predicate) {
for (Iterator<T> iterator=begin(); iterator != end(); ++iterator) {
if (predicate(*iterator)) {
return iterator;
}
}
return end();
}
// predicate is a functor that is supposed to return a boolean value
here's the function object:
class findBond{
Bond& bond;
public:
findBond( Bond& bond1) : bond(bond1) {}
bool operator() (Bond& bond1){
return bond==bond1;
}
};
I'm trying to use them here:
void InvestmentBroker :: addBond(const string& name, double value, int amount ){
Bond bond = *new Bond(name, value, amount);
if (bondsDatabase.find(findBond(bond)) != bondsDatabase.end()) {
//throw an exception
} else {
// insert bond to dataBase
}
}
I included the needed files so it's not about the includes
What's wrong? What am I missing here?
Your find method takes a const Predicate&
as its argument. That means you can only call const
methods of the predicate. However, your functor's call operator is not declared const
. You can fix your problem by declaring it const
like this:
bool operator() (Bond& bond1) const {/* do stuff */ }
That const
at the end of the declaration means that you can't modify this
from within the function, which in turn means you can call the function on a const
object.
It looks like a constness issue to me - findBond
should be const
on most arguments.
Change findBond
to this
class findBond{
const Bond& bond;
public:
findBond( const Bond& bond1) : bond(bond1) {}
bool operator() const ( const Bond& bond1){
return bond==bond1;
}
};
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.