Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

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

views
     
TScheatz92
post Jul 9 2013, 11:30 PM, updated 13y ago

Getting Started
**
Junior Member
149 posts

Joined: Oct 2006


» Click to show Spoiler - click again to hide... «


you guys can check my code in the spoiler.
this is the error part.

CODE
void operator /();


if i try to compile will get the "too few parameters in the operator function" error.
what does this mean?
what did i do wrong. thanks.
dstl1128
post Jul 10 2013, 09:44 AM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
Just imagine, how to 'divide' without anything to begin with?


TScheatz92
post Jul 10 2013, 01:13 PM

Getting Started
**
Junior Member
149 posts

Joined: Oct 2006


QUOTE(dstl1128 @ Jul 10 2013, 09:44 AM)
Just imagine, how to 'divide' without anything to begin with?
*
what do you mean?
care to elaborate.

this code is to change the arithmetic symbol function.
the divide is gonna change to +.

so everytime i use / it actually add the numbers.

dstl1128
post Jul 10 2013, 01:32 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
QUOTE(cheatz92 @ Jul 10 2013, 01:13 PM)
what do you mean?
care to elaborate.

this code is to change the arithmetic symbol function.
the divide is gonna change to +.

so everytime i use / it actually add the numbers.
*
How to add numbers if you don't accept any?
TScheatz92
post Jul 10 2013, 01:38 PM

Getting Started
**
Junior Member
149 posts

Joined: Oct 2006


QUOTE(dstl1128 @ Jul 10 2013, 01:32 PM)
How to add numbers if you don't accept any?
*
i did. its in the class itself. check my code in the spoiler.
the setdata segment. because its within the class i dont have to recieve any parameter.
and the others are working. just that one is not working.

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.
Loseeker
post Jul 10 2013, 02:51 PM

Apa Macam
*****
Senior Member
899 posts

Joined: Jul 2008
From: The Land of Smile
QUOTE(Eventless @ Jul 10 2013, 02:16 PM)
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 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.

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.

This post has been edited by Loseeker: Jul 10 2013, 02:57 PM
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.
dstl1128
post Jul 10 2013, 04:14 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
QUOTE(Eventless @ Jul 10 2013, 03:07 PM)
Not exactly.

Some operators does not need a second object or values(++,--).
*
++ and -- do have optional parameters - for differentiating post & pre increment/decrement.

dstl1128
post Jul 10 2013, 04:16 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
QUOTE(cheatz92 @ Jul 10 2013, 01:38 PM)
i did. its in the class itself. check my code in the spoiler.
the setdata segment. because its within the class i dont have to recieve any parameter.
and the others are working. just that one is not working.
*
Binary operators (eg. +, -, *, /) need two parameters. If it is class member function, then the first one is inherent - just provide another one.

If it is non-class member function, then it still need two parameters and friend for touching private parts.



 

Change to:
| Lo-Fi Version
0.0152sec    0.40    5 queries    GZIP Disabled
Time is now: 15th December 2025 - 05:50 PM