Gig*_*ion 6 c++ compiler-errors codeblocks specifier
我是 C++ 新手,我一直在读这本书。我读了几章,想到了自己的想法。我尝试编译下面的代码,但出现以下错误:
||=== 构建:密码调试(编译器:GNU GCC 编译器)===| /Users/Administrator/Desktop/AppCreations/C++/Password/Password/main.cpp|5|错误:C++ 需要所有声明的类型说明符| ||=== 构建失败:1 个错误,0 个警告(0 分钟,2 秒)===|。
我不明白代码有什么问题,有人可以解释一下有什么问题以及如何修复它吗?我读了其他帖子,但我无法理解。
谢谢。
#include <iostream>
using namespace std;
main()
{
string password;
cin >> password;
if (password == "Lieutenant") {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
您需要包含字符串库,您还需要为 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
}
Run Code Online (Sandbox Code Playgroud)
您需要声明 main 的返回类型。这应该始终是int合法的 C++。在许多情况下,主程序的最后一行是return 0;- 即成功退出。除此以外的任何内容都0用于指示错误情况。