错误: : 不是类或命名空间名称

Error: : is not a class or namespace name

本文关键字:命名空间 错误      更新时间:2023-10-16

我的任务是创建一个程序来管理餐厅的活动,其中包括用户协调功能和许多其他功能。我只需要在 1 个.cpp文件中提交此作业,所以我尝试将代码压缩为仅 1 个文件:D。以下是我到目前为止在创建用户界面时所做的工作:

#include <iostream>
using namespace std;
class UserInterface{
public:
    typedef UserInterface super;
    static int user_input;
    static void menu(){
        int input;
        print();
        setInput(input);
        execute();
    }
    static void print(){
        cout << "Welcome to the Restaurant Managing program!" << endl;
        cout << "Please enter your ID. The ID of chef is 0 and the ID of customers is a positive integer: ";
    };
    static bool setInput(int input){
        cin >> input;
        if (input >= 0){
            user_input = input;
            return true;
        }
        else{
            cout << "Invalid input!" << endl;
            return false;
        }
    };
    static void execute(){
        switch (user_input){
        case 0:
            break;
        default:
            Customer::menu();
            break;
        }
    };
};

class Customer :public UserInterface{
public:
    static void print(){
        cout << "1.Exit" << endl << "2.Make an order" << endl << "3.View orders" << endl << "4.Change order" << endl;
        cout << "Please enter your ID: ";
    }
    static bool setInput(int input){
        cin >> input;
        if (input >= 1 && input <= 4){
            user_input = input;
            return true;
        }
        else{
            cout << "Invalid input!" << endl;
            return false;
        }
    };
    static void exit(){
        super::menu();
    };
    static void makeOrder(){};
    static void viewOrder(){};
    static void changeOrder(){};
    static void execute(){
        switch (user_input){
        case 1:
            exit();
            break;
        }
    };
};
int UserInterface::user_input;
int main(){
    int input;
    UserInterface::menu();
    system("pause");
}

问题是当我编译此代码时出现此错误:

Error   1   error C2653: 'Customer' : is not a class or namespace name  

有人可以告诉我我在这里做错了什么,我还能做些什么来改进我的代码吗?

当你尝试调用Customer::menu();时,编译器还没有看到Customer类。这正是错误消息所说的:"客户不是类或命名空间名称。

即使它知道Customer是一个类的名称,它也不会知道它是否真的具有menu()成员函数。它还不知道Customer是从UserInterface派生的,并继承了menu函数。

一种解决方案是将UserInterface::execute拆分为一个声明,它可以在Customer的定义之前,以及一个定义,可以在Customer定义之后

class UserInterface { // start definition of UserInterface class
// ...
    static void execute(); // declaration of execute() function
}; // end definition of UserInterface class

class Customer : public UserInterface { // start definition of Customer class
// ...
}; // end definition of Customer class

void UserInterface::execute() { // start definition of execute() function
    switch (user_input){
    case 0:
        break;
    default:
        Customer::menu();
        break;
    }
}; // end definition of execute() function

另一种解决方案是只调用函数而不进行限定:

static void execute(){
    switch (user_input){
    case 0:
        break;
    default:
        menu();
        break;
    }
};

这取决于你真正想要实现的目标。也许您以后想使menu成为非静态函数并在派生类中重写它,那么您不能再使用这个简单的解决方案。

一般来说,我会说你应该重新考虑你的整个类设计,因为它看起来不必要地复杂。


PS:注意编译器警告:

warning C4101: 'input': unreferenced local variable
warning C4700: uninitialized local variable 'input' used

定义此函数

static void execute(){
    switch (user_input){
    case 0:
        break;
    default:
        Customer::menu();
        break;
    }
};

在定义之后,类Customer

考虑到在下面的函数中input局部变量的声明

static void menu(){
    int input;
    print();
    setInput(input);
    execute();
}

没有意义,因为函数中未使用此变量。