tags:

views:

176

answers:

3

Hello, I did not see anything that address my particular code problem.

I have a bool function in a class; the bool function is named:

bool Triplet::operator ==(const Triplet& operand)const {
    if( (data[0] == operand.data[0]) &&
            (data[1] == operand.data[1]) &&
            (data[2] == operand.data[2]))
        return true; 
        ...

And I am trying to call it in Main but having problems just getting the call right. Apparently if I leave out any thing in the wording it gives an error that I have too few aruguments and if I try to use the entire wording of the function, I get the error that I need a semi-colon, but I already have a semi-colon at the end of the call, so I know that something else is wrong and I simply cannot figure out what is wrong! Any help would be appreciated!

Thanks in advance.

+11  A: 

You can call it using == (that's what operator overloading does; it overloads the meaning of the operator):

Triplet a;
Triplet b;

if (a == b) //< calls your operator==

You can also call it using the function call syntax:

if (a.operator==(b)) //< also calls your operator==

but you don't usually want or need to do that.

One case you might need to do that is if for some reason you have an operator template that has template parameters that can't be deduced from the arguments (that's rare for operator==, but I've seen that used for operator[] before).

James McNellis
Thanks, I used the Triplet a; Triplet b; etc. and it worked. Thank you all so much for the answers. I really appreciated it!
Gerri
+7  A: 

That is a C++ operator overload, it is designed so it can change the behaviour of the normal operator:

Triplet a, b;
if (a == b) // calls Triplet::operator==()

If you really want to, you can call the operator by name:

if (a.operator==(b))
Simon Buchan
Except that it would be `a.operator==(b)`.
Troubadour
Thanks, I used the Triplet a; Triplet b; etc. and it worked. Thank you all so much for the answers. I really appreciated it!
Gerri
@Troubadour: D'oh! Teach me to code 10mins after waking up :)
Simon Buchan
+2  A: 

bool is just the return type. It has nothing to do with how the function as called. As in James' example, this function is an operator, therefore you will use the operator like you would in normal syntax to call it.

DoctorT
Thanks, I used the Triplet a; Triplet b; etc. and it worked. Thank you all so much for the answers. I really appreciated it!
Gerri