C++ 函数和“未找到标识符”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8548387/
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
C++ Functions and 'identifier not found' error
提问by Eray
I'm a C++ learner.I'm trying to code a basic C++ project. It's basic automation system . It has a navigation menu.
我是一名 C++ 学习者。我正在尝试编写一个基本的 C++ 项目。它是基本的自动化系统。它有一个导航菜单。
Choose an option :
1 ) Add a new customer
2 ) List all customers
...
There will be a nav
variable, which containing user's choice. This is my source code :
将有一个nav
变量,其中包含用户的选择。这是我的源代码:
#include <stdio.h>
#include <clocale>
int nav;
void main()
{
setlocale(LC_ALL,"turkish");
navigasyon();
switch (nav) // checking 'nav' variable which assigned in navigasyon()
case "1" :
printf("Now you are adding a new customer");
break;
case "2" :
printf("Now you are listing all customers");
break;
}
void navigasyon()
{
printf("Choose an option \n");
printf("1 ) Add a new customer\n");
printf("2 ) List all customers\n");
scanf("%d", &nav); // assigning '`nav' variable
}
In shortly,
in main() , navigasyon() will show navigation menu, user choose 1 or 2 and then navigasyon() assinging it to nav
. Lastly checking nav
with switch-case.
很快,在 main() 中, navigasyon() 将显示导航菜单,用户选择 1 或 2 然后 navigasyon() 将其分配给nav
。最后检查nav
开关盒。
But i'm getting 'navigasyon': identifier not found
error.
但我收到'navigasyon': identifier not found
错误。
回答by kol
You should declare navigasyon
before main
.
你应该navigasyon
在之前声明main
。
回答by Mike S.
You need to declare the function first
你需要先声明函数
After your #includes statements write this
在你的#includes 语句之后写这个
void navigasyon();
Alternatively you can place the function before the main. In C++ a function has to be declared before being used, unless it's placed before the main.
或者,您可以将函数放在 main 之前。在 C++ 中,函数必须在使用前声明,除非它放在 main 之前。
回答by n0rd
You use navigasyon
function in main
before defining it. You need either to switch places of main
and navigasyon
, or declare navigasyon
before main
.
在定义它之前使用navigasyon
函数main
。您需要切换main
和的位置navigasyon
,或者navigasyon
在之前声明main
。
回答by Christian Horsdal
Place 'navigasyon' above main.
将“navigasyon”放在主要上方。
C++ demands that functions are declared before they are used. This can done by placing the whole function before the usage or by forward declaration, where you only have the signature before.
C++ 要求函数在使用前先声明。这可以通过将整个函数放在使用之前或通过前向声明来完成,其中您之前只有签名。
回答by Ernest Friedman-Hill
You need a declaration for the navigasyon()
function at the beginning of your file, so the compiler knows about it when it compiles main()
:
您需要navigasyon()
在文件开头为函数声明,以便编译器在编译时知道它main()
:
void navigasyon();
回答by m42e
you need to declare the method before using it. Add the following Line before your main:
您需要在使用之前声明该方法。在 main 之前添加以下行:
static void navigasyon();
This declares navigasyon as a local only (static) function. For further use you should move the decleration to a .h file, remove the static and of course include this .h file.
这将 navigasyon 声明为仅本地(静态)函数。为了进一步使用,您应该将 decleration 移动到 .h 文件,删除静态文件,当然包括这个 .h 文件。