C++ “非静态成员函数的无效使用”这是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11869266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:38:55  来源:igfitidea点击:

"invalid use of non static member function" What is this?

c++classfunctionsyntax

提问by Carpetfizz

EDIT: thanks for all the speedy responses, I have a much better understanding of this concept now. Also, I'll try to make my error messages more clear next time. EDIT: updated with my newest code. the error happens on line 18. Also, I'm beginning to wonder if my latest issue has to do with the original class itself?

编辑:感谢所有快速响应,我现在对这个概念有了更好的理解。另外,下次我会尽量让我的错误信息更清晰。编辑:用我最新的代码更新。错误发生在第 18 行。另外,我开始怀疑我的最新问题是否与原始类本身有关?

I'm trying to teach myself classes and objects in C++. I did it once by just declaring a voidfunction, outputting something on the screen, calling the object in main and everything worked fine.

我正在尝试在 C++ 中自学类和对象。我做了一次,只是声明了一个void函数,在屏幕上输出一些东西,在 main 中调用对象,一切正常。

Now, I wanted to expand upon this and make a simple addition thing. However, I get a couple errors on Code Blocks:

现在,我想对此进行扩展并做一个简单的加法。但是,我在代码块上遇到了一些错误:

error: invalid use of non-static member function 'int Addition::add(int, int)'
error: no matching function for call to 'Addition::add()'

Here's my code:

这是我的代码:

#include <iostream>

using namespace std;

class Addition {
public:
int add (int x, int y) {
int sum;
sum=x+y;
return sum;
}
};

int main()
{
int num1;
int num2;
int ans=addobj.add(num1,num2);
Addition addobj;
addobj.add(num1,num2);
cout<<"Enter the first number you want to add"<<endl;
cin>>num1;
cout<<"Enter the second number you want to add"<<endl;
cin>>num2;
cout<<"The sum is "<<ans<<endl;
}

回答by Kiril Kirov

One of the most important things, a developer should learn to do is to read compiler's messages. It's clear enough:

开发人员应该学会做的最重要的事情之一是阅读编译器的消息。已经很清楚了:

error: no matching function for call to 'Addition::add()'

Your function in your class is

你在课堂上的功能是

int add (int x, int y)

it takes 2 arguments and you pass none:

它需要 2 个参数,而您没有传递:

addobj.add();


You have 2 options:

您有 2 个选择:

  • create and initialize xand yinside your mainand pass them as arguments
  • make addwithout parameters, create xand yinside add's body, as their values are taken from user input.
  • 创建和初始化xy在您的内部main并将它们作为参数传递
  • makeadd不带参数, createxyinsideadd的主体,因为它们的值取自用户输入。

In this case, as the function's name is add, I'd chose the first option:

在这种情况下,由于函数的名称是add,我选择了第一个选项:

  • declare int x, y;inside your main
  • read the user input inside the main(the part, where you use cinand cout)
  • pass the xand yas arguments to addlike this: addobj.add( x, y );
  • store the result (if needed), like this: int result = addobj.add( x, y );
  • int x, y;在你的内部声明main
  • 读取main(部分,您使用cincout)内的用户输入
  • xy作为参数传递给add这样的:addobj.add( x, y );
  • 存储结果(如果需要),如下所示: int result = addobj.add( x, y );

回答by Ernest Friedman-Hill

You declared a method add(int, int)that takes two integers as arguments; you have to supply those arguments when you call it. It would be nice to print the returned value, as well:

你声明了一个add(int, int)接受两个整数作为参数的方法;您必须在调用时提供这些参数。打印返回值也很好:

Addition addobj;
std::cout << addobj.add(1, 2) << std::endl;

回答by Ed S.

Your addfunction takes two arguments, yet you call it with none, so no matching function could be found. You must call the function as it was declared, i.e.,

你的add函数有两个参数,但你没有调用它,所以找不到匹配的函数。您必须按照声明的方式调用该函数,即

addobj.add(1, 2);

回答by mathematician1975

Your function takes two arguments and yet you call it without providing them. You need to provide the two integer arguments that your function requires. To be useful you should store the result too. Something like this

您的函数需要两个参数,但您在不提供它们的情况下调用它。您需要提供函数所需的两个整数参数。为了有用,您也应该存储结果。像这样的东西

 int a = 1;
 int b = 2;
 int result = addjobs.add(a,b);