gpt4 book ai didi

c++ - 为什么是 exit(0);给我一个 std :string. .. 错误?

转载 作者:太空宇宙 更新时间:2023-11-03 10:46:55 26 4
gpt4 key购买 nike

我是 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 成员中找不到时抛出一个嘶嘶声。

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

关于c++ - 为什么是 exit(0);给我一个 std :string. .. 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com