Let Wallet be a class that represents the amount of coins and bills in a purse. The class has
got a data element amount of type long. The binary operator += shall be overloaded as an
inline method. Identify the error in the following definition.

Wallet& operator+=(Wallet& w)
{ // add the amount contained in another purse
Wallet* this;
this->amount += w.amount;
w.amount = 0L;
return *this;
}

Can somebody explain me this?

Recommended Answers

All 2 Replies

It's simple. This is a method of the Wallet class that takes a Wallet as a parameter, takes its amount of money and adds it to the Wallet on which the method is invoked (this). The other wallet has its amount set to 0.

To find the error, create the class with the minimum requirements and try to compile it! You will see the error, but it's not so difficult to see. Then, this homework question will be completed.

Yet another tip: the only correct statement is return *this; ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.