#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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With