错误:无效使用非静态成员函数C ++

时间:2018-09-13 12:15:49

标签: c++ function class

我有两个要链接调用的类(主->执行->计算)。但是,问题是当我使用时:

&calculate::addition;

即使编译器未返回任何错误,也不会调用Addition。如果我尝试删除对

的引用
calculate::addition;

编译器返回错误

error: invalid use of non-static member function ‘void calculate::addition(double&, double, double)’
     case 'a' : *calculate::addition; break;
                            ^~~~~~~~

在void之前使用static尝试过,结果与引用相同。

#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;   
}

2 个答案:

答案 0 :(得分:0)

您的代码中有几个问题。让我们开始吧:

error: invalid use of non-static member function ‘void calculate::addition(double&, double, double)’
 case 'a' : *calculate::addition; break;

这意味着您必须创建一个calcute实例或使用static void addition(double&, double, double);之类的静态标记方法

因此将您的班级更改为

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);
};

下一个问题是,在switch语句中,您仅创建指向函数的指针

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;
    }   
}

这从不执行任何功能,而只是创建一个立即被丢弃的功能指针。

为了使您的代码正常工作,请考虑以下代码(请注意代码中的注释,这些注释解释了所需的更改):

#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;   
}

希望这有助于您理解问题。

答案 1 :(得分:0)

要调用class计算中的任何方法,您必须先声明一个变量,然后再调用该方法,例如:

calculate c;
double a,b,res;
c.addition(a,b,res);

或者您将方法定义为静态函数,在这种情况下,调用将像这样

calculate::addition(a,b,res);