c++ - 错误: C++ requires a type specifier for all declarations

标签 c++ compiler-errors codeblocks specifier

我是 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/

相关文章:

c++ - 运算符无故更改类的参数 (*int)

sdk - CUDA 5.0 : checkCudaErrors fails to find correct “check” method if class has a “check” method

在 C 中使用 while(scanf) 时出现代码块错误

c++ - "lib"头的库文件名在codeblocks和vs9编译器中无法识别

c++ - undefined reference 和运算符 << 对我

c++ - 为什么 sizeof Derived Class 是 8?

冲突类型中的 C 错误

java - 直方图放大问题

winapi - #define _UNICODE 不适用于 MinGW + CodeBlocks

c++ - 从右值到右值引用的 reinterpret_cast