Error when using "find" from STL Algorithm
Sep 17, 2013 at 2:51pm UTC
Hello. I'm doing an assignment that requires me to write a program to manage customer's bank accounts. OOP concepts are required.
when I tried to use the find algorithm to look for the bank account that I have to perform an action on, I get the following error message:
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_algo.h:212:0 /Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_algo.h:212: error: no match for 'operator==' in '__first. __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Person**, _Container = std::vector<Person*, std::allocator<Person*> >]() == __val'
I can't figure out what it means, and can't find an explanation online too.
1 2 3
vector<Person*> accounts;
vector<Person*>::iterator iter1;
vector<Person*>::iterator iter2;
I'm using a vector of pointers for the accounts stored.
Here's the code that gives the issue stated above.
1 2 3 4 5 6 7 8 9
if (action == "Withdraw" ) {
iter1 = find(accounts.begin(), accounts.end(), Person(name));
(*iter1)->withdrawMoney(amount);
}
if (action == "Deposit" ) {
iter2 = find(accounts.begin(), accounts.end(), Person(name));
(*iter2)->depositMoney(amount);
}
thanks in advance!
Last edited on Sep 17, 2013 at 2:52pm UTC
Sep 17, 2013 at 2:55pm UTC
you cannot compare a pointer to Person
with a Person
object. the error tells you that an operator for this doesn't exist
Sep 17, 2013 at 3:10pm UTC
is there another way to look for the
Person
object that I need to find?
I've tried doing this:
1 2 3 4 5 6 7 8 9
if (action == "Withdraw" ) {
iter1 = find(accounts.begin(), accounts.end(), accounts.getName());
(*iter1)->withdrawMoney(amount);
}
if (action == "Deposit" ) {
iter2 = find(accounts.begin(), accounts.end(), accounts.getName());
(*iter2)->depositMoney(amount);
}
but it doesn't work.
Sep 18, 2013 at 8:03am UTC
The problem is the
pointer to
Person
. What it does is basically
1 2 3 4
Person *a;
Person b;
if (a == b) // This is not allowed
...
I'd think that the best would be to not use pointer, i.e.
vector<Person> accounts;
Note that you have to implement the member function
bool operator ==(const Person &) const
Alternative you could implement a global
operator ==
that takes a pointer and a reference:
1 2
bool operator ==(const Person *, const Person &);
bool operator ==(const Person &, const Person *);
Last edited on Sep 18, 2013 at 8:03am UTC
Sep 19, 2013 at 6:37am UTC
I have to use pointers though.
I've solved the problem already!
Thanks!
Sep 19, 2013 at 7:46am UTC
I've solved the problem already!
How?
Sep 19, 2013 at 12:38pm UTC
I used a for loop with iterators.
1 2 3 4 5 6 7
if (action == "Withdraw" ) { //if the operation is to withdraw money,
for (iter1 = accounts.begin(); iter1 != accounts.end(); iter1++) { //iterate through the vector to look for the correct name
if ((*iter1)->getName() == name) {
cout << (*iter1)->withdrawMoney(amount) << endl; //call the function withdrawMoney on the element found
}
}
}
Topic archived. No new replies allowed.