"undefined"变量和参数
"undefined" variables and arguments
我是一名编写第一个函数的学生,所以我相信这将是我的一个明显错误。在第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;
}
让我告诉您代码中的问题。
- 您需要告诉编译器num1和num2是什么
- 您的代码还有一个更严重的问题。你必须知道,大多数情况下,编译器在编译过程中是逐行进行的。所以,当他到达你的主体时,他不知道什么是减法。你应该告诉他这是一个函数,否则这将是一个编译时错误。提示-试着定义你在代码中写的每一个可变的东西。编译器自己无法推导出任何东西
- 正如在评论中提到的,我发现了另一个问题,要在下一行中移动,您应该写"\n"(它是反斜杠(
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;
}
Num1和num2未定义,函数中的一个将屏蔽另一个。
相关文章:
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 扩展可变参数模板中的变量名称
- 在类构造函数中定义结构变量的参数
- 带有整数的变量参数列表
- 转发变量参数列表以模拟 std::thread
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- 存储稍后要转发的变量参数
- C++具有模板成员变量的类. 和参数内存输出
- 我应该如何在没有变量的情况下将相同的参数传递给 CMAKE 中的多个目标?
- 为变量模板的每个参数调用模板函数
- 具有推导参数的模板函数指针数组变量
- 函数参数变量总是需要 & 或 * 运算符吗?
- 为什么 beginthreadex 线程参数变量在父线程中没有更新
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 如何在函数中使用非参数变量?
- 即将将引用作为函数中的参数传递以更改参数变量的值
- 基于不断变化的参数/变量的XML get值
- cocos2d-x v3 CallFunc作为参数/变量如何
- 在编译时使用c++元编程中的运行时参数(变量)
- 我可以返回一个非参数变量吗?