Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

operator + overload question

Why does this code give the error message "IntelliSense: too many parameters for this operator function"

int operator+(PerfectNum a,PerfectNum b)
{
    return (a.thenum+b.thenum);
}

PerfectNum is a regular class, and thenum is an int. This method is in the class.

like image 866
soandos Avatar asked Nov 17 '25 14:11

soandos


2 Answers

You're defining this as a member function, right?

In that case, the left hand side is simply *this:

// .h
class PerfectNum
{
    public:
    int operator+(PerfectNum other) const;
};

// .cpp
int PerfectNum::operator+(const PerfectNum &other) const
{
    return this->thenum + other.thenum;
}
like image 58
Platinum Azure Avatar answered Nov 20 '25 04:11

Platinum Azure


You need to make your operator a non-member function.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!