2013-10-15 21 views
1

我是C++新手。我決定不看下一個教程,並通過製作一個有趣的心智讀者應用程序來使用我的技能。然而,即使我已經解決了大多數錯誤,但我仍然對自己感到滿意,但仍然有一個涉及退出功能。我閱讀了它的C++文檔,我不確定我做錯了什麼。我沒有退出(0);.我有一個很奇怪的錯誤,那就是:爲什麼退出(0);給我一個std:string ...錯誤?

no match for call to '(std::string {aka std::basic_string<char>}) (int) 

我已經在網上搜索,但我仍然不知道問題是什麼。我的錯誤是在第59行(標記代碼):

#include <iostream> 
#include <string> 
#include <cstdlib> 
using namespace std; 

int main() 
{ 
    //declaring variables to be used later 
    string name; 
    string country; 
    int age; 

    //header goes below 
    cout << "#######################################"; 
      " @@@@@@@@@@@@ MIND READER @@@@@@@@@@@@" 
      "#######################################\n\n"; 

    //asks if the user would like to continue and in not, terminates 
    cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl; 
    cout << "If you do not choose to proceed, this program will terminate." << endl; 
    string exitOrNot; 
    //receives user's input 
    cin >> exitOrNot; 
    //deals with input if it is 'y' 
    if (exitOrNot == "y"){ 
     cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n"; 

     //asks questions 
     cout << "Firstly, please enter your full name, with correct capitalisation:\n\n"; 
     cin >> name; 

     cout << "Now please enter the country you are in at the moment:\n\n"; 
     cin >> country; 

     cout << "This will be the final question; please provide your age:\n\n"; 
     cin >> age; 

     //asks the user to start the sync 
     cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n"; 
     string proceed; 
     cin >> proceed; 
     //checks to see if to proceed and does so 
     if (proceed == "p"){ 
      //provides results of mind read 
      cout << "Sync complete." << endl; 
      cout << "Your mind has been synced and read.\n\n"; 
      cout << "However, due to too much interference, only limited data was aquired from your mind." << endl; 
      cout << "Here is what was read from your mind:\n\n"; 

      //puts variables in sentence 
      cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n"; 

      cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl; 
      //terminates the program the program 
      string exit; 
      cin >> exit; 
      if (exit == "e"){ 
       exit(0);  // <------------- LINE 59 
      } 

     } 

    } 
    //terminates the program if the input is 'n' 
    if (exitOrNot == "n"){ 
     exit(0); 
    } 

    return 0; 
} 

感謝

+3

請發表您的代碼。 – Barmar

回答

4

從具有相同名稱的外部範圍的局部變量exit陰影其他標識符。

要以更小的例子示出了:

int main() 
{ 
    int i; 
    { 
     int i; 
     i = 0; // assign to the "nearest" i 
     // the other i cannot be reached from this scope 
    } 
} 

由於僅exit可見是std::string類型的對象,編譯器看到exit(0)operator()(int)呼叫並拋出一個噓聲像配合時它沒有找到std::string成員之一。

您可以限定名稱(std::exit(0);)或重命名變量。由於您的所有代碼都在main中,因此您可以簡單地說return 0;

+0

嗨。我很抱歉,但是我對你的答案感到困惑,因爲我實際上是新版波士頓C++課程的15集。然而,下面的人也回答了我的問題,並且我評論道。你能幫我嗎?謝謝。 – IrshadAM

+0

@IrshadAM如果你告訴我什麼是我的答案混淆,我會很樂意修改。 – jrok

0

由於您將標準關鍵字聲明爲局部變量的名稱,因此存在問題。 現在,由於局部變量的類型是sting,因此無法將其作爲其值。

+0

謝謝,但你能告訴哪個變量是罪魁禍首嗎? – IrshadAM

+0

@IrshadAM代碼中只有一個'exit'局部變量。 – jrok

+0

恩,謝謝你們所有的幫助。 問題:親愛的上帝,我的愚蠢。 那麼我的程序現在的作品,我只需要修復標題。 感謝您的幫助! 我愛堆交換! 對不起,垃圾郵件btw! – IrshadAM

3

嘗試使用return 0;return EXIT_SUCCESS;。這完全一樣。另外,您只能將一個詞輸入cin。相反,使用getline(cin, string name);如果仍然無法正常工作,您getline(cin, string name);前添加cin.ignore();,像這樣:

//stuff 
string country; 
cout << "Now please enter the country you are in at the moment:\n\n"; 
cin.ignore(); 
getline(cin, country); 
//stuff 
return 0; 
相關問題