appleJuice
appleJuice

Reputation: 137

no match for ‘operator ==’ in ‘a == b’

#include <iostream>
using namespace std;

class family
{
private:
        double weight;
        double height;
public:
        family(double x,double y); 
        ~family();
        double getWeight();
        double getHeight();
        double setWeight();
        double setHeight();
        bool operator==(const family &,const family &); 
};

bool family::operator ==(const family &a,const family &b) 
{
        return(a.getWeight() == b.getWeight());
}    

family::family(double x, double y)
{
        weight = x;
        height = y;
}

double family::getWeight()
{
        return weight;
}

double family::getHeight()
{
        return height;
}

family::~family(){}

int main()
{
    family a(70.0,175.2);
    family b(68.5,178.2);

    if(a==b)
    cout << "A is bigger than B" << endl;
    else
    cout << "A is smaller than B" << endl;

return 0;
}

I want to use method for overloading equal operator. However, I have an error message

"no match for ‘operator ==’ in ‘a == b’"

Why this error message come up ? Furthermore, I want to know why there is reference symbol "&" in (const family &,const family &). Please, give me some advice for modifying my code b.b.

Upvotes: 4

Views: 1483

Answers (4)

Dr. Debasish Jana
Dr. Debasish Jana

Reputation: 7118

Two possibilities: (1) If you want to keep your definition same: declare as non-member and friend:

friend bool operator==(const family &,const family &); 

define as:

bool operator ==(const family &a,const family &b) 
{
        return(a.getWeight() == b.getWeight());
}   

(2) Declare as a member (implicit argument is the current object pointed by this): declare as non-member and friend:

bool operator==(const family &); 

define as:

bool family::operator ==(const family &a) 
{
        return(this->getWeight() == a.getWeight());
}   

Upvotes: 0

Wlerin
Wlerin

Reputation: 260

Why this error message come up ?

If you implement a binary operator as a member function, it only receives the right-hand side as an argument, the left-hand side is the calling object. If you write:

a == b

the compiler looks for a function that meets either:

(return type) (type of lhs)::operator==( (type of rhs));

or

(return type) operator==( (type of lhs), (type of rhs) );

Note: (return type) could be anything, though normally you'd want to return bool here. It doesn't affect what the compiler looks for when making the function call.

Your function signature instead is:

(return type) (type: family)::operator==( (type: family), (type: family) );

This is expecting three arguments (one implied)!


Furthermore, I want to know why there is reference symbol "&" in (const family &,const family &).

const family & is the type of argument the function accepts. It receives objects of family type by reference (that is, it uses the original objects rather than making a copy of them), and it promises not to modify them (const). The compiler will enforce this promise. Since the function doesn't need to modify either object, and there's no reason to make a full copy of either, this is exactly the right signature to use. For a non-member function.

For a member function you have to modify it slightly:

class family
{   // ...
    bool operator==(
    // ...
}

This is fine so far, we don't have to change anything. Your parameter list should only include the right-hand side argument, so:

bool operator==(const family&)

But we're not quite done. Remember how the non-member function uses "const family&" as the parameter type? Somehow we need to mark the calling object as const too. We do this by adding const at the very end:

bool operator==(const family&) const;

(The calling object is already available as though by reference.)


When you go to write the function itself, simply use:

bool family::operator==(const family &rhs) const {
    ...
}

Then for the body of the function, you can either use the members of the calling object and the rhs directly, or call their relevant functions, like so:

    return weight == rhs.weight; // direct member access

or

    return getWeight() == rhs.getWeight(); // using functions

Upvotes: 4

Nipun Talukdar
Nipun Talukdar

Reputation: 5387

You may do the below changes in the code:

 double getWeight() const; 

. .

bool operator==(const family &);

. .

bool family::operator==(const family &b) 
{
        return weight == b.getWeight();
}   

Upvotes: 1

billz
billz

Reputation: 45410

If you implement operator== as a member function, it only takes one parameter.

Or in practice, you could implement it as a free function

class family
{
private:
        double weight;
        double height;
public:
        family(double x,double y); 
        ~family();
        double getWeight() const;
        double getHeight() const;
        double setWeight();
        double setHeight();
};

bool operator==(const family &a,const family &b)
{
   return a.getWeight() == b.getWeight();
}

Update: AS operator== takes const family objects, you need to make getWight()/getHeight() function const.

Upvotes: 2

Related Questions