简体   繁体   English

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

[英]error: invalid use of non-static member function C++

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.尝试在 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;   
}

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);这意味着您必须创建一个计算实例或使用静态标记方法,例如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下一个问题是,在您的 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;
    }   
}

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.希望这有助于理解您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM