我是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;
}
由于
答案 0 :(得分: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;
,因此您可以简单地说{{1}}。
答案 1 :(得分:3)
尝试使用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;
答案 2 :(得分:0)
问题在于,因为您将标准关键字声明为局部变量的名称。 现在由于局部变量是sting类型,因此无法将其作为其值。