我是一名学生,我正在编写我的第一个函数,所以我确信这对我来说显然是错误的。 在第13行,我在参数中遇到错误,告诉我未定义num1和num2。据我了解传递参数,第9行应该告诉第13行num1和num2是什么(1和2)。既然它不起作用,我显然会误会它的工作原理。
如果有人可以解释我在做什么错,我将不胜感激。谢谢你!
#include <iostream>
#include <string>
using namespace std;
int main()
{
Subtract(1, 2);
return 0;
}
int Subtract(num1, num2) //num1 and num2 are undefined.
{
int num1;
int num2;
int x;
x = num1 - num2;
cout << x << "/n";
return 0;
}
答案 0 :(得分:1)
让我告诉您代码中的问题。
int Subtract(int , int); // This is must before main if you defined subtract later.
int main()
{
Subtract(1, 2); // Compiler don't know what is subtract. As you defined Subtract later. The compiler doesn't know what is Subtract. To overcome this you need to declare a function before main.
return 0;
}
int Subtract(int num1, int num2) // Here you need to tell that they are an integer.
{
// int num1;
/// int num2; // once you told that num1 and num2 are an integer no need to do this. If you will try this. It will be a compile time error. As you already made num1 and num2 variable above. So can't declare variable twice.
int x;
x = num1 - num2;
cout << x << "/n";
return 0;
}
答案 1 :(得分:-1)
Num1和num2没有定义,函数in将屏蔽另一个。