gpt4 book ai didi

c++ - 在学习教程时,我收到错误 : C++ requires a type specifier for all declarations

转载 作者:行者123 更新时间:2023-11-30 02:34:04 30 4
gpt4 key购买 nike

我有一些 pythonphp 的经验,但现在决定学习一些 C++。我正在学习关于 http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm 的教程.但我收到“错误:C++ 需要所有声明的类型说明符”。

教程内容如下:

例如,下面的代码定义了一个名为colors 的颜色枚举和color 类型的变量c。最后,c 被赋值为“blue”。

enum color { red, green, blue } c;
c = blue;

这是我的代码:

#include <iostream>
using namespace std;

enum color {red, blue, green} c;

c = green;

int main()
{
cout << c << endl;
return 0;
}

当我尝试编译时出现此错误:

someuser@somemac:~/cpp/cpptut$ g++ enum.cpp
enum.cpp:6:1: error: C++ requires a type specifier for all declarations
c = green;
^
1 error generated.

在我看来,我所做的一切都完全按照教程中的说明进行。教程有错误吗?

最佳答案

你不能在全局范围内有松散的语句,你需要把它放在一个函数中,或者在定义时初始化变量。

所以要么

enum color {red, blue, green} c = green;

enum color {red, blue, green} c;

int main()
{
c = green;
...
}

关于c++ - 在学习教程时,我收到错误 : C++ requires a type specifier for all declarations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34873606/

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