2016-03-12 48 views
1

我的任務是創建一個程序來管理餐廳的活動,其中包括用戶reconizing功能和許多其他features.I我需要的只有1個.cpp文件提交這項任務所以我試圖將代碼壓縮成只有一個文件: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 

是否有人可以告訴我,我什麼在這裏做錯了什麼,我還能做些什麼來改進我的代碼?

+2

不能調用派生類的靜態函數的方式。設計是怪異的順便說一句。 –

回答

2

當你嘗試調用Customer::menu();,編譯器目前還沒有看到該Customer類。這正是錯誤信息所說的:「客戶不是類或名稱空間名稱」。

即使知道Customer是一個類的名稱,它也不知道它是否真的具有menu()成員函數。它不知道的還以爲CustomerUserInterface派生和繼承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成爲一個非靜態函數並在派生類中覆蓋它,那麼你就不能再使用這個簡單的解決方案。

一般來說,我會說你應該重新考慮你的整個一流的設計,因爲它似乎是不必要的複雜。


P.S:注意編譯器的警告:

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

定義這個功能

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

    } 

}; 

定義類Customer後。

考慮到局部變量input的該宣言中了以下功能

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

沒有意義,因爲該變量沒有在函數中使用。

相關問題