iterator to a vector of a struct type?

I'm working on a program where I have a vector full of <myClassType> structs.

I'm trying to insert items into a vector, searching first through the vector to make sure the value isn't already in the vector before inserting it. The "find" function isn't working properly.

I keep getting C2678 "binary '==': no operator found which takes a left-hand operand of type "myClassType" or there is no conversion errors in Visual Studio 2010.

I know it's something having to do with the find function and my iterators, but I can't, for the life of me, figure out what it is.

I've tried switching to const_iterators, but I get the same error message.

Can anyone help? I'd appreciate it!

1
2
3
4
5
6
7
8
9
10
11
void foo::insert(const myClassType& en)
{	
        vector<myClassType>::iterator it;
        vector<myClassType>::iterator it1;
        it = find(Table.begin(), Table.end(), en.memberOne);
        it1 = find(Table.begin(), Table.end(), "Blank");
        if (it != Table.end())
                cout << "Error:  Already used!" << endl;
        else (it1 == Table.end())
              cout << "Error:  Table full!" << endl;
}
My guess is that you don't have operator== defined for myClassType. Without it, there's no way find can tell whether two elements are equal or not.
1
2
3
4
if (it != Table.end())
                cout << "Error:  Already used!" << endl;
        else (it1 == Table.end())
              cout << "Error:  Table full!" << endl;


You can't compare non-standard data types without operator overloading.

http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading

Here's a resource, or you can just do your own googling
Wow. That was amazingly fast. Thank you both very very much.

I had seen something about operator overloading in my search, but I wasn't certain if that applied to my dilemma or not. Apparently it does!

Thanks for the help. :)

Susan
Topic archived. No new replies allowed.