【问题标题】:Something was not decleared in this scope在此范围内未声明某些内容
【发布时间】:2022-01-12 03:50:36
【问题描述】:

对于我的代码,我想将已完成的函数调用到 switch case 函数中。但是每当我尝试运行时,它都会显示我没有在范围内声明。

下面是我的代码:

void mainpage(Admin a)
{

        time_t t = time(NULL);
        tm* timePtr = localtime(&t);
        cout<< "Date : "<<timePtr->tm_mday<<"-"<<timePtr->tm_mon+1<<"-"<<timePtr->tm_year + 1900<<"\nTime : "<< timePtr->tm_hour <<":"<<timePtr->tm_min;
        int choice=0;

            cout<<"\n-----------------------------";
            cout<<"\nMovie Ticket Booking System";
            cout<<"\n-----------------------------";
            cout<<"\nUSER INFO";
            cout<<"\n <1> ADMIN";
            cout<<"\n <2> CUSTOMER";
            cout<<"\n <3> CURRENTLY SHOWING";
            cout<<"\n <4> Exit \n\n";
            cout<<"Enter Your Choice : ";

            cin>>choice;

            switch(choice)
            {
            case 1:
                a.admin();
                break;
            case 2:
                c.customer();
                break;
            case 3:
                s.shows();
                break;
            case 4:
                cout<< "\nThank you for choosing us. We hope that you will enjoy the movie!";
                break;

            default:
                cout<<"\n WRONG OPTION";
                mainpage(a);
            }
}

对于案例 2 和案例 3,这些是有问题的行。我想展示整个代码,但系统不让我看,因为它有太多的代码。

c.customer() 的函数调用:

void customer()
{   Customer c;
    Admin a;
    int ch;
    cout<<"\nCUSTOMER PAGE";
    cout<<"\n<1> Existing Customer";
    cout<<"\n<2> Register New Customer";
    cout<<"\nEnter Your Choice : ";
    cin>>ch;
    cin.ignore();

s.shows() 的函数调用:

void shows()
{   
        int choice,time;
        char buy;
        ifstream file("Currentlyshowing.txt", ios::in);

        if (!file.is_open()) {cout<< "error open file";}

        for(int i=0; i<=5; i++)
        {
            getline(file,moviename[i]);
        }
        for(int i=0; i<=5; i++)
        {
            for(int j=0; j<=5; j++)
            {
                file>>show[i][j];
            }
        }

提前致谢!

【问题讨论】:

  • 在询问相关问题时显示整个错误消息。 cs 的定义在哪里,您在其上调用这些成员函数 customer()shows()?你是不是真的打算在 Admin 类型的对象 a 上调用这些函数?请注意,您对mainpage 的递归调用可能会导致堆栈溢出,除非编译器选择将其优化为尾递归。要打破这种情况,我需要做的就是输入“fred”作为我的选择,然后让程序崩溃或让它在繁忙循环中冻结。
  • cs 是什么?
  • C:\Users\user\Desktop\try2.cpp In function 'void mainpage(Admin)': 279 5 C:\Users\user\Desktop\try2.cpp [Error] 'c' was not declared in this scope 282 11 C:\Users\user\Desktop\try2.cpp [Error] 's' was not declared in this scope @paddy 这些是错误
  • edit your question 添加详细信息,而不是使用 cmets。阅读我的答案,根据您提供的有关代码的有限信息,我可以提供对正在发生的事情的最佳解释。

标签: c++ scope switch-statement


【解决方案1】:

让我们再简化一点。解释您的问题,我们有:

case 2:
    c.customer();  //<-- 'c' is not declared at this scope
    break;
case 3:
    s.shows();     //<-- 's' is not declared at this scope
    break;

但为什么案例 1 没问题?

case 1:
    a.admin();     //<-- just fine!
    break;

好吧,标识符a 作为参数传递到函数中。因此,该值被视为在范围内。换句话说,它指的是一个具有已知类型且可由函数访问的真实对象。

那么,这对值 cs 意味着什么?可能这是两件事之一:

  1. 您在另一个函数中创建了对象cs,并希望它们在函数mainpage 中可见。但是,它们不是作为参数提供的,而且由于变量也是非全局的,所以它们在函数的范围内是不可见的。

  2. 标识符cs 实际上是拼写错误,您打算改用a

编译器告诉你的主要事情是你的函数不知道cs是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2016-08-09
    • 2019-02-17
    • 2021-01-07
    • 2016-10-27
    • 2016-06-03
    相关资源
    最近更新 更多