错误: : 不是类或命名空间名称
Error: : is not a class or namespace name
我的任务是创建一个程序来管理餐厅的活动,其中包括用户协调功能和许多其他功能。我只需要在 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();
}
没有意义,因为函数中未使用此变量。
相关文章:
- 使用命名空间时出现多个定义错误
- 使用 make 编译 MPI,几个命名空间错误,例如"错误:未知类型名称'使用'?
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- C++ Visual Studio 错误:名称后跟'::'必须是类或命名空间名称 (DirectX 11)
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- C++命名空间中调用 void 函数的错误
- 无法获取 wig %ignore 以忽略命名空间中的类,从而导致编译错误
- 名称后跟"::"必须是类或命名空间错误,即使"::"后面是类名
- fstream.h 中的 STL 命名空间错误与 qmake/make.
- 在 main() 中使用 getline() 函数时出现命名空间错误
- C++命名空间错误:不是命名空间的成员
- 为什么我得到这个不是类或命名空间错误的'enum'?
- C++命名空间错误
- C++ Visual Studio 2015 CLR Windows 窗体命名空间错误
- LNK 2019命名空间错误
- Gsoap:如何解决引用组时的命名空间错误
- 变量已定义,命名空间错误
- c++初学者:使用命名空间错误
- Swig命名空间错误?不是一个有效的基类
- 命名空间错误'Not a namespace-name'