簡體   English   中英

為什么退出(0); 給我一個std:string ...錯誤?

[英]Why is exit(0); giving me a std:string… error?

我是C ++的新手。 我決定不看下一本教程,而是通過制作一個有趣的Mind Reader應用程序來使用我的技能。 我對自己感到滿意,盡管我已經解決了大多數錯誤,但我仍然有一個關於退出功能的問題。 我已經閱讀了C ++文檔,但不確定自己做錯了什么。 我確實退出了(0);。 我有一個很奇怪的錯誤,它是:

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;
}

謝謝

局部變量exit在外部作用域中隱藏了具有相同名稱的其他標識符。

為了說明一個較小的示例:

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

由於唯一可見的exitstd::string類型的對象,因此編譯器將exit(0)視為對operator()(int)的調用,並在std::string成員中找不到一個時拋出hissy fit 。

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

嘗試使用return 0; return EXIT_SUCCESS; 完全一樣。 另外,您只能在cin輸入一個單詞。 而是使用getline(cin, string name); 如果仍然不起作用,請添加cin.ignore(); 在您的getline(cin, string name); , 像這樣:

//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;

問題來了,因為您聲明了標准關鍵字作為局部變量的名稱。 現在,由於局部變量的類型為sting,因此無法將其用作其值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM