我是 C++ 新手,我一直在读这本书。我读了几章,想到了自己的想法。我尝试编译下面的代码,但出现以下错误:
||=== Build: Debug in Password (compiler: GNU GCC Compiler) ===| /Users/Administrator/Desktop/AppCreations/C++/Password/Password/main.cpp|5|error: C++ requires a type specifier for all declarations| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|.
我不明白代码有什么问题,有人可以解释一下有什么问题以及如何修复它吗?我读了其他帖子,但无法理解。
谢谢。
#include <iostream>
using namespace std;
main()
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
}
最佳答案
您需要包含字符串库,您还需要为您的 main 函数提供一个返回类型,并且您的实现可能需要您为 main 声明显式返回语句(如果您没有显式地声明,某些实现会添加隐式返回语句)提供一个);像这样:
#include <iostream>
#include <string> //this is the line of code you are missing
using namespace std;
int main()//you also need to provide a return type for your main function
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
return 0;//potentially optional return statement
}
关于c++ - 错误: C++ requires a type specifier for all declarations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28162788/