C语言 空声明中无用的类存储说明符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2743949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:19:41  来源:igfitidea点击:

useless class storage specifier in empty declaration

cenums

提问by ant2009

gcc 4.4.1 c89

海湾合作委员会 4.4.1 c89

I have the following code:

我有以下代码:

static enum states
{
    ACTIVE,
    RUNNING,
    STOPPED,
    IDLE
};

And I get a warning:

我收到警告:

useless class storage specifier in empty declaration

However, if i remove the static keyword I don't get that warning.

但是,如果我删除 static 关键字,则不会收到该警告。

I am compiling with the following flags:

我正在编译以下标志:

-Wall -Wextra

Many thanks for any suggestions,

非常感谢您的任何建议,

采纳答案by tvanfosson

You get the message because you're not actually declaring, you're only definingsomething, namely an enumeration named "states". You can later use this definition to declare a variable of that type. That variable may be a static or instance variable, but the definition doesn't need (and shouldn't have) the storage specifier attached to it.

您收到消息是因为您实际上并没有声明,您只是在定义一些东西,即一个名为“states”的枚举。您可以稍后使用此定义来声明该类型的变量。该变量可能是静态或实例变量,但定义不需要(也不应该有)附加到它的存储说明符。

回答by CB Bailey

Your enumdeclaration is defining a type, but it is not also declaring an object of that type.

您的enum声明定义了一种类型,但它并未同时声明该类型的对象。

staticonly applies to variables and functions so, as the compiler says, it is useless in the context in which you have it.

static仅适用于变量和函数,因此正如编译器所说,它在您拥有它的上下文中是无用的。

回答by Johannes Schaub - litb

What do you want the static to do? It serves there to give variables defined in the declaration internal linkage:

你想让静态做什么?它用于提供声明内部链接中定义的变量:

static enum states { ... } a;

As a shortcut for

作为快捷方式

enum states { ... };
static enum states a;

Giving "a" internal linkage. But since you don't define a variable there in your code, it is useless in fact (if not illegal).

给出“一个”内部链接。但是由于您没有在代码中定义变量,因此实际上它是无用的(如果不是非法的)。

回答by Johannes Schaub - litb

Try:

尝试:

static enum states
{
    ACTIVE,
    RUNNING,
    STOPPED,
    IDLE
} avar;

which actually creates a static variable called avar. Only variables can be static, not types.

它实际上创建了一个名为 avar 的静态变量。只有变量可以是静态的,不能是类型。