错误"菜单"未在此范围内声明C++

error 'menu' was not declarated in this scope C++

本文关键字:范围内 声明 C++ 菜单 错误      更新时间:2023-10-16

下面是代码

#include <iostream>
#include <cstdlib>
#include <math.h>
using namespace std;
int x,d,p,r,se,me;
int y,s,c,op;
void calculator1()
{
    {
        start2:;
        system("color 0A") ;
        cout << "Primul Nr. " ; cin >> x ;
        cout << "Nr Doi. ";     cin >> y ;
        start1:;
        system("color 0A");
        cout << "n 1. Suma";
        cout << "n 2. Diferenta";
        cout << "n 3. Produsul";
        cout << "n 4. Catul (Fara REST)";
        cout << "n 5. Catul (Cu REST)";
        cout << "n Ce operatie ? ";cin >> op ;
        s = x + y;
        d = x - y;
        p = x * y;
        c = x / y;
        r = x % y;
        if(op == 1)
        {
            cout << "n Suma n" << s ;
            system("pause");
            cout << "1. Inapoi la calulator" <<endl;
            cout << "2. Inapoi la meniul principal";
            cin >> me;
            if(me==1){
                goto start2;
                     }
            if(me==2){
                    system("cls");
                     }
        }
        else if(op == 2)
        {
            cout << "n Dif. n" << d ;
            system("pause");
            cout << "1. Inapoi la calulator" <<endl;
            cout << "2. Inapoi la meniul principal";
            cin >> me;
            if(me==1){
                goto start2;
                     }
            if(me==2){
               system("cls");
                     }
        }
        else if(op == 3)
        {
            cout << "n Prod n" << p ;
            system("pause");
            cout << "1. Inapoi la calulator" <<endl;
            cout << "2. Inapoi la meniul principal";
            cin >> me;
            if(me==1){
                goto start2;
                     }
            if(me==2){
               system("cls");
                     }
        }
        else if(op == 4)
        {
            cout << "n Cat. n" << c ;
            system("pause");
            cout << "1. Inapoi la calulator" <<endl;
            cout << "2. Inapoi la meniul principal";
            cin >> me;
            if(me==1){
                goto start2;
                     }
            if(me==2){
               system("cls");
                     }
        }
        else if(op == 5)
        {
            cout << "n Cat. " << c << " rest " << r << "n ";
            system("pause");
            cout << "1. Inapoi la calulator" <<endl;
            cout << "2. Inapoi la meniul principal";
            cin >> me;
            if(me==1){
                    system("cls");
                goto start2;
                     }
            if(me==2){
            system("cls");
            menu();
                     }
        }
        else
        {
        system("color cf");
        cout << "n Alegere Gresita n" ;
        system("pause");
        system("CLS");
        goto start1;
        }
}

和延续

} void menu() { system("color 0A"); cout << "1. Calculator (Simplu)n"; cout << "nAlege. "; cin >> se ; if(se==1) { system("cls"); calculator1(); } } int main() { menu(); }

完整代码和编译器错误:

"error: 'menu'未在此范围内声明"

我使用的是Windows 7 Ultimate和编译器GNU GCC compiler

请帮帮我!

谢谢

在函数calculator1的主体内使用函数menu。但是函数menu是在函数calculator1定义之后声明的。所以编译器知道calculator1

中的menu是什么意思

为了避免错误,可以在函数calculator1之前声明函数menu,然后在函数calculator1之后定义它

函数递归地调用自己也是一个坏主意。尝试编写不使用递归和goto语句的程序

在C和c++中,每个函数在使用之前都必须声明。这意味着在调用menu()的函数之前,必须有一个menu声明。
否则,当编译器到达调用menu()的代码时,它将不知道menu的存在。

这段代码不能编译:

int main()
{
    menu();
    return 0;
}
void menu()
{
    cout << "This is the menu functionn";
}

下面的代码将编译:

void menu(); /* menu declaration - this line tells the compiler that menu
              * is a void function which takes no arguments
              */
int main()
{
    menu();
    return 0;
}
void menu()
{
    cout << "This is the menu functionn";
}

要解决这个问题,请将void menu();行放在calculator1行之前。