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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:46:54  来源:igfitidea点击:

C++ Functions and 'identifier not found' error

c++

提问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 navvariable, 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 navwith switch-case.

很快,在 main() 中, navigasyon() 将显示导航菜单,用户选择 1 或 2 然后 navigasyon() 将其分配给nav。最后检查nav开关盒。

But i'm getting 'navigasyon': identifier not founderror.

但我收到'navigasyon': identifier not found错误。

回答by kol

You should declare navigasyonbefore 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 navigasyonfunction in mainbefore defining it. You need either to switch places of mainand navigasyon, or declare navigasyonbefore 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 文件。