嘿伙计们,我正在努力学习C ++,直到我碰到这堵墙,我才做得很好..
我收到两个错误: 错误:未在此范围内声明'enter' 错误:“Satisfies”未在此范围中声明|
这是我的档案。那是为什么?
include <iostream>
using namespace std;
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter(); break;
case 2: Satisfies(); break;
case 3: break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}
答案 0 :(得分:2)
你必须在使用之前声明这些函数,例如在main之前,并使用正确的调用函数语法。
例如
//...
int Enter(){ return 0; }
int Satisfies(){ return 0; }
//...
int main()
{
//...
case 1:
Enter();
//...
答案 1 :(得分:1)
在使用函数之前,您需要声明函数。所以把它放在main
上面:
int Enter();
int Satisfies();
您可以保留 definitions (实际包含在调用函数时运行的代码的位)所在的位置。或者您可以将这些函数移到main
之上,因为函数定义也是声明。
编译器在尝试调用函数之前需要查看这些位,以便它可以知道它需要什么参数以及将返回的内容。
答案 2 :(得分:0)
您需要在使用之前声明(或声明并确定)某个函数。你需要用()进行函数调用。像Enter()和Satisfied()。如果你想学习优秀的编程和C编码,然后去C ++阅读&#34; C for Dummies&#34;丹·戈金金我最喜欢的编码书。
您有3种方法可以执行此操作并修复代码:
<强> 1。编写原型定义:
#include <iostram>
int Enter();
int Satisfies();
using namespace std;
int main()
{
//bla
}
int Enter(){ return 0; }
int Satisfies(){ return 0; }
<强> 2。创建一个function.h文件并将声明放在那里。将其保存在与c / cpp文件相同的文件夹中
然后包含在您的代码中
#include "function.h"
3将您的函数按c / cpp文件中的执行顺序排列。必须在使用之前声明函数。例如:强>
void Enter()
{
//bla
}
void Satisfied()
{
//blub
}
int main()
{
Enter();
Satisfied();
}
更棘手的例子,当一个函数(Satisfied)使用另一个函数(Enter)时,必须在Satisfied函数之前声明Enter函数:
void Enter()
{
//bla
}
void Satisfied()
{
//blubb
Enter(); //now Enter must be declared before is Satisfied() is defined, so it must be "over" it in the source like in this example
}
int main()
{
Enter();
Satisfied();
}
答案 3 :(得分:0)
函数调用在函数名称无论是否接受任何参数之后都需要()
。另外,您需要在main()
include <iostream>
using namespace std;
int Enter();
int Satisfies();
int main() {
while (1){
char menu;
cin>>menu;
switch (menu){
case 1: Enter();
break;
case 2: Satisfies();
break;
case 3:
break;
};
};
}
int Enter(){
return 0;
}
int Satisfies(){
return 0;
}