Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 too few parameters in the operator function, help please. c++

views
     
Eventless
post Jul 10 2013, 02:16 PM

Look at all my stars!!
*******
Senior Member
2,643 posts

Joined: Jan 2003
http://www.cplusplus.com/doc/tutorial/classes2/
Based on the link above, you can't do that with /. You need to have a parameter when overloading /. You also won't be able to do the following in your code:
CODE
/obj;


I have a feeling that operator overloading was not designed for this purpose. You should be using member functions to do those kind of operations as it will be more readable and most of the values are already inside the object.
Eventless
post Jul 10 2013, 03:07 PM

Look at all my stars!!
*******
Senior Member
2,643 posts

Joined: Jan 2003
QUOTE(Loseeker @ Jul 10 2013, 02:51 PM)
Eventless is right! You've been using the overloading operator the wrong way. Simply put, the overloading operator should be used to operate on variables contained in 2 different objects of the same type, NOT on the different variables within the same object. That is why the division operator demand an arqument, which for your case, is another object of type NewCalculator.
*
Not exactly.

Some operators does not need a second object or values(++,--).

For operators that work with other objects or values(+,-,*,...), it can work with other object and data types as long as you overload an operator to accept the target data type or object. It does not need to be same object type.
Eventless
post Jul 10 2013, 03:15 PM

Look at all my stars!!
*******
Senior Member
2,643 posts

Joined: Jan 2003
QUOTE(Loseeker @ Jul 10 2013, 02:51 PM)
      Try to create an object for both n1 and n2 (example: NewCalculator obj_n1, obj_n2) instead of putting them in the same object (your existing object-->  NewCalculator obj). Only then you overload the operator to add/divide/multiply/subtract the two numbers within the two different objects. If you insist on putting the two numbers together within the same object, then you should just create and use member functions to operate the two number instead of operator overloading as Eventless mentioned above.
*
Good written code should be easily translated to English or any language of choice. Notice in the example above, it says to add 2 numbers together instead of add 2 calculators together to get a result. You don't add(operator) 2 calculators together in real life. A calculator is supposed to add(member function) 2 numbers together to get a result.

 

Change to:
| Lo-Fi Version
0.0145sec    0.61    6 queries    GZIP Disabled
Time is now: 17th December 2025 - 02:51 AM