Reputation: 51
I have two classes that I want to chain-call (main -> execute -> calculate). However, the issue is that when I use:
&calculate::addition;
Addition will not be called even if the compiler doesn't return any error. If I try to remove the reference to
calculate::addition;
The compiler returns error
error: invalid use of non-static member function ‘void calculate::addition(double&, double, double)’
case 'a' : *calculate::addition; break;
^~~~~~~~
Tried using static before void with the same result as using reference.
#include <iostream>
class execute{
public:
void exec(char);
}jalan;
class calculate {
public:
void addition(double&, double, double);
void substraction(double&, double, double);
void multiply(double&, double, double);
void division(double&, double, double);
};
int main(void){
static double a, b;
static double result;
std::cout << "Type a, b, c, or d" << std::endl;
std::cout << "a. Addition\nb. Substraction\nc. Multiply\nd. Division" << std::endl;
std::cout << "Your Input: ";
static char option;
option = getchar();
std::cout << "First value: ";
std::cin >> a;
std::cout << "Next value: ";
std::cin >> b;
jalan.exec(option);
std::cout << result << std::endl;
return 0;
}
void execute::exec (char option){
switch(option){
case 'a' : &calculate::addition; break;
case 'b' : &calculate::substraction; break;
case 'c' : &calculate::multiply; break;
case 'd' : &calculate::division; break;
}
}
void calculate::addition(double& result, double a, double b){
result = a+b;
}
void calculate::substraction(double& result, double a, double b){
result = a-b;
}
void calculate::multiply(double& result, double a, double b){
result = a*b;
}
void calculate::division(double& result, double a, double b){
result = a/b;
}
Upvotes: 0
Views: 13939
Reputation:
You have several issues in your code. Lets start:
error: invalid use of non-static member function ‘void calculate::addition(double&, double, double)’ case 'a' : *calculate::addition; break;
This means you have to create an instance of calculate or mark the method with static like static void addition(double&, double, double);
So change your class to
class calculate {
public:
static void addition(double&, double, double);
static void substraction(double&, double, double);
static void multiply(double&, double, double);
static void division(double&, double, double);
};
The next issue is that in your switch statement you only create pointers to functions
void execute::exec (char option){
switch(option){
case 'a' : &calculate::addition; break;
case 'b' : &calculate::substraction; break;
case 'c' : &calculate::multiply; break;
case 'd' : &calculate::division; break;
}
}
This never executes a function, but only creates a function pointer which is discarded right away.
In order to get your code to work, consider this code (note the comments in the code, which explain changes needed):
#include <iostream>
class execute
{
public:
void exec(char, double&, double, double);
}jalan;
class calculate {
public: // added static keyword so you do not need to create a class instance
static void addition(double&, double, double);
static void substraction(double&, double, double);
static void multiply(double&, double, double);
static void division(double&, double, double);
};
int main(void){
static double a, b;
static double result;
std::cout << "Type a, b, c, or d" << std::endl;
std::cout << "a. Addition\nb. Subtraction\nc. Multiply\nd. Division" << std::endl;
std::cout << "Your Input: ";
static char option;
option = getchar();
std::cout << "First value: ";
std::cin >> a;
std::cout << "Next value: ";
std::cin >> b;
jalan.exec(option, result, a, b); // you need to pass the arguments which you want to use and modify
std::cout << result << std::endl;
return 0;
}
void execute::exec (char option, double& res, double a, double b){
switch(option){ // changed the function pointers to actual calls to the functions
case 'a' : calculate::addition(res, a, b); break;
case 'b' : calculate::substraction(res, a, b); break;
case 'c' : calculate::multiply(res, a, b); break;
case 'd' : calculate::division(res, a, b); break;
}
}
void calculate::addition(double& result, double a, double b){
result = a+b;
}
void calculate::substraction(double& result, double a, double b){
result = a-b;
}
void calculate::multiply(double& result, double a, double b){
result = a*b;
}
void calculate::division(double& result, double a, double b){
result = a/b;
}
Hope this helps understanding your problems.
Upvotes: 2
Reputation: 75
To call any method in class calculate you have to declare a variable first then call the method for example:
calculate c;
double a,b,res;
c.addition(a,b,res);
or you define the methods as static functions, in that case the calling will be like that
calculate::addition(a,b,res);
Upvotes: 0