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
"invalid use of non static member function" What is this?
提问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 void
function, 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
x
andy
inside yourmain
and pass them as arguments - make
add
without parameters, createx
andy
insideadd
's body, as their values are taken from user input.
- 创建和初始化
x
并y
在您的内部main
并将它们作为参数传递 - make
add
不带参数, createx
和y
insideadd
的主体,因为它们的值取自用户输入。
In this case, as the function's name is add
, I'd chose the first option:
在这种情况下,由于函数的名称是add
,我选择了第一个选项:
- declare
int x, y;
inside yourmain
- read the user input inside the
main
(the part, where you usecin
andcout
) - pass the
x
andy
as arguments toadd
like this:addobj.add( x, y );
- store the result (if needed), like this:
int result = addobj.add( x, y );
int x, y;
在你的内部声明main
- 读取
main
(部分,您使用cin
和cout
)内的用户输入 - 将
x
和y
作为参数传递给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 add
function 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);