Outline ·
[ Standard ] ·
Linear+
too few parameters in the operator function, help please. c++
|
TScheatz92
|
Jul 9 2013, 11:30 PM, updated 13y ago
|
Getting Started

|
» Click to show Spoiler - click again to hide... « CODE #include<iostream> #include<conio.h>
using namespace std;
class NewCalculator{
private:
double sum , n1, n2;
public:
NewCalculator():sum(0),n1(0),n2(0){}
void menu();
double getsum();
void setdata();
void operator +(); void operator -();
void operator *();
void operator /();
};
void NewCalculator::menu(){
cout<<"Plese select operation(enter 1 - 4): "<<endl<<"1. Division"<<endl; cout<<"2. Multlipication"<<endl<<"3. Substraction"<<endl<<"4. Addition"<<endl; }
void NewCalculator::setdata(){
cout<<"Please enter first number: " <<endl; cin>>n1; cout<<"Please second first number: " <<endl; cin>>n2;
}
void NewCalculator::operator +(){
sum = n1 / n2; } void NewCalculator::operator -(){
sum = n1 * n2; }
void NewCalculator::operator *(){
sum = n1 - n2; }
void NewCalculator::operator /(){
sum = n1 + n2;
}
double NewCalculator::getsum(){
return sum; }
void main(){
NewCalculator obj;
int c;
obj.setdata();
l1: obj.menu(); cin>>c;
switch(c){
case 1 :{ +obj; break; }
case 2:{ -obj; break; }
case 3:{ *obj; break; }
case 4:{ -obj; break; }
default:{ cout<<"Please choose between 1 to 4!"<<endl; system("pause"); system("cls"); goto l1; } }
cout<<obj.getsum(); getch();
}
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
|
Jul 10 2013, 09:44 AM
|
|
Just imagine, how to 'divide' without anything to begin with?
|
|
|
|
|
|
TScheatz92
|
Jul 10 2013, 01:13 PM
|
Getting Started

|
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
|
Jul 10 2013, 01:32 PM
|
|
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
|
Jul 10 2013, 01:38 PM
|
Getting Started

|
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
|
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.
|
|
|
|
|
|
Loseeker
|
Jul 10 2013, 02:51 PM
|
|
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
|
Jul 10 2013, 03:07 PM
|
|
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
|
Jul 10 2013, 03:15 PM
|
|
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
|
Jul 10 2013, 04:14 PM
|
|
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
|
Jul 10 2013, 04:16 PM
|
|
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.
|
|
|
|
|