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 a method to overload an equal operator. However, I have an error message

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

Why is this error message appearing? In addition, I want to know why there is a reference character "&" in (const family &, const family &). Please give me some tips on changing my bb code

+4
source share
4 answers

Why is this error message appearing?

If you implement the binary operator as a member function, it takes only the right side as an argument, and the left side is the caller. If you write:

a == b

the compiler is looking for a function that matches either:

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

or

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

: ( ) , bool . , .

:

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

( )!


, , "&" in (const family &, const family &).

const family & - , . family (.. , ), promises (const). . , , , . , .

- :

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

, . , :

bool operator==(const family&)

. , non-member "const family &" ? , const. , const :

bool operator==(const family&) const;

( .)


, :

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

rhs :

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

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

operator== -, .

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();
}

: AS operator== const, getWight()/getHeight() const.

+2

You can make the following changes to the code:

 double getWeight() const; 

. .

bool operator==(const family &);

. .

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

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

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

defined as:

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

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

bool operator==(const family &); 

defined as:

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

All Articles