错误'menu'未在此范围C ++中声明

时间:2014-07-20 19:15:43

标签: c++ compiler-errors

这是代码

#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(); }

完整代码和编译器错误:

&#34;错误:&#39;菜单&#39;未在此范围内宣布&#34;

我使用的是Windows 7旗舰版和编译器GNU GCC编译器

请帮助我!

由于

2 个答案:

答案 0 :(得分:0)

您在函数menu的正文中使用函数calculator1。但是函数menu是在函数calculator1的定义之后声明的。因此,编译器确实知道menu

正文中名称calculator1的含义

如果不发生错误,您可以在函数menu之前声明函数calculator1,并在函数calculator1之后定义

这也是一个坏主意,函数会递归调用它们。 尝试编写没有递归和goto syayements的程序。

答案 1 :(得分:0)

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

此代码无法编译:

int main()
{
    menu();
    return 0;
}

void menu()
{
    cout << "This is the menu function\n";
}

此代码将编译:

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 function\n";
}

要解决您的问题,请将行void menu();放在calculator1的开头。