1

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

谢谢

4

3 回答 3

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可见的是 type 的对象std::string,因此编译器将exit(0)其视为对成员的调用并在它在成员operator()(int)中找不到一个时抛出一个嘶嘶声。std::string

您可以限定名称 ( std::exit(0);) 或重命名变量。而且由于您的所有代码都在其中,main因此您可以简单地说return 0;

于 2013-10-15T19:06:22.283 回答
2

尝试使用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;
于 2014-06-24T00:57:47.340 回答
0

问题出现了,因为您将标准关键字声明为局部变量的名称。现在,由于局部变量是 sting 类型的,因此无法将其作为其值。

于 2013-10-15T19:17:35.627 回答