2012-12-15 40 views
-2

我想C++,仍然學生在uni,無法找到錯誤來自哪裏。有人可以幫忙嗎?需要調試'預期的非限定id之前')'令牌',第一次C++用戶

class M() { 
    static bool m() { 
     cout << "Hello, do you want to tell me your name (y or n)"; 
     char answer = 0; 
     int times = 1; 
     while(times < 3) { 
      cin >> answer; 
      switch(answer){ 
       case 'y' : 
        return true; 
       case 'n' : 
        return false; 
       default : 
        cout << "I am sorry, I don't understand that."; 
        times += 1; 
      } 
      cout << "Your time's up."; 
      return false; 
     }  
    } 
} 

int main() { 
    M::m(); 
}; 

回答

1

這是在這條線:

class M() { 

你不把類名定義後的括號內。將其更改爲:

class M { 

有一些更多的問題與您的代碼(類右大括號,等以後分號),工作代碼看起來像這樣:

class M { 
public: 
    static bool m() { 
     std::cout << "Hello, do you want to tell me your name (y or n)"; 
     char answer = 0; 
     int times = 1; 
     while(times < 3) { 
      std::cin >> answer; 
      switch(answer){ 
       case 'y' : 
        return true; 
       case 'n' : 
        return false; 
       default : 
        std::cout << "I am sorry, I don't understand that."; 
        times += 1; 
      } 
      std::cout << "Your time's up."; 
      return false; 
     } 
     // You need this so you won't get warnings. 
     return false; 
    } 
}; // Don't forget this semicolon! 

int main() { 
    M SomeObject; 
    SomeObject::m(); 
}; 
+0

權,但是如何創建給定類的實例,因爲除此之外,main()會報告沒有創建M的實例。 「無法調用成員函數'bool M :: m()'without object」 –

+0

我已經更新了答案。 – Izhaki

+0

謝謝,我不知道語法如何,事情開始有意義,只是std :: cin/cout實際上是Class :: methodname?謝謝您的幫助 –

相關問題