Error when using "find" from STL Algorithm

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
you cannot compare a pointer to Person with a Person object. the error tells you that an operator for this doesn't exist
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.
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
I have to use pointers though.

I've solved the problem already!
Thanks!
I've solved the problem already!
How?
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.