简体   繁体   English

“无用的类型限定符”错误

[英]“useless type qualifier” error

I get the following error with the following code. 我使用以下代码得到以下错误。 I tried to figure out where the problem is over Google, but I didn't find anything helpful. 我试图找出问题在谷歌的哪个方面,但我没有找到任何有用的信息。

Compiling /home/tectu/projects/resources/chibios/ext/lcd/touchpad.c
In file included from /home/tectu/projects/resources/chibios/ext/lcd/touchpad.c:1:0:
/home/tectu/projects/resources/chibios/ext/lcd/touchpad.h:17:1: warning: useless type qualifier in empty declaration [enabled by default]

Here's the the code from line 12 to line 17 from touchpad.h : 这是touchpad.h第12行到第17行的代码:

volatile struct cal {
    float xm; 
    float ym; 
    float xn; 
    float yn; 
};

And here's how I use this struct inside touchpad.c : 以下是我在touchpad.c使用此结构的方法:

static struct cal cal = { 
    1, 1, 0, 0  
};

Can anyone show me the light? 有谁能告诉我光明? :D :d

volatile as a qualifier can be applied to a particular instance of structure. volatile作为限定符可以应用于特定的结构实例。
You are applying it to a type which is useless and the compiler correctly points it out. 您正在将它应用于无用的类型,并且编译器正确指出它。

volatile qualifies a variable, not a type. volatile限定变量,而不是类型。

Doing: 这样做:

static volatile struct cal {
    float xm; 
    float ym; 
    float xn; 
    float yn; 
} cal;

would be legal, as would: 是合法的,因为:

struct cal {
    float xm; 
    float ym; 
    float xn; 
    float yn; 
};

static volatile struct cal cal;

The volatile keyword makes sense with an object. volatile关键字对于对象是有意义的。 Not a type definition. 不是类型定义。

You don't get an error, just a warning. 你不会得到错误,只是一个警告。

And that applies to how you declare your struct cal : it is not volatile by itself; 这适用于你如何声明你的struct cal :它本身并不易变; the volatile only applies to a concrete variable definition. volatile仅适用于具体的变量定义。

So in static struct cal cal , your variable cal is just static , but not volatile . 所以在static struct cal cal ,你的变量cal只是static ,但不是volatile

In that sense, the volatile declaration is, as the warning says, useless. 从这个意义上说,正如警告所说, volatile声明是无用的。

易失性关键工作应该与实际变量一起使用,而不是类型定义。

You can't attach a volatile qualifier to a struct declaration. 您不能将volatile限定符附加到struct声明。

But, contrary to the other answers, you can in fact use the volatile qualifier on types, but not on structs . 但是,与其他答案相反,您实际上可以在类型上使用volatile限定符,但不在 结构上使用 If you use typedef , you can create a volatile type for a struct. 如果使用typedef ,则可以为结构创建volatile类型。

In the following example, Vol_struct is actually not volatile, as in the poster's question. 在下面的示例中, Vol_struct实际上不是易失性的,如海报的问题所示。 But Vol_type will create volatile variables without further qualification: Vol_type将创建volatile变量而无需进一步限定:

/* -------------------------------------------------------------------- */
/* Declare some structs/types                                           */
/* -------------------------------------------------------------------- */

/* wrong: can't apply volatile qualifier to a struct
   gcc emits warning: useless type qualifier in empty declaration */
volatile struct Vol_struct
{
    int x;
};

/* effectively the same as Vol_struct */
struct Plain_struct
{
    int x;
};

/* you CAN apply volatile qualifier to a type using typedef */
typedef volatile struct
{
    int x;
} Vol_type;


/* -------------------------------------------------------------------- */
/* Declare some variables using the above types                         */
/* -------------------------------------------------------------------- */
struct Vol_struct g_vol_struct;                     /* NOT volatile */
struct Plain_struct g_plain_struct;                 /* not volatile */
volatile struct Plain_struct g_vol_plain_struct;    /* volatile */
Vol_type g_vol_type;                                /* volatile */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C 错误:类型名称需要说明符或限定符 - C error: type name requires a specifier or qualifier 没有类型说明符的类型限定符 - type qualifier without type specifier 限制限定符编译错误 - restrict qualifier compilation error C中的数据类型或限定符是多长? - Is long a data type or qualifier in C? 这个错误意味着什么:“错误:'type_name'之前的预期说明符 - 限定符列表”? - What does this error mean: “error: expected specifier-qualifier-list before 'type_name'”? 错误:0:2:“属性”:无法初始化此类限定符(无论 GLSL 版本从 1.1 到 1.5 使用的是什么) - ERROR: 0:2: 'attribute' : cannot initialize this type of qualifier (whatever GLSL version used from 1.1 till 1.5) 使用来自另一个结构的元素构造struct(错误:在'type'之前的期望说明符 - 限定符列表) - Making struct with element from another struct (error: expected specifier-qualifier-list before 'type') C ++编译器面临错误“多次指定类型限定符” - facing error “type qualifier specified more than once” with c++ compiler _Atomic类型限定符和类型说明符之间有区别吗? - Is there a difference between the _Atomic type qualifier and type specifier? 将 _Atomic 类型限定符应用于不完整类型是否有效? - Is applying _Atomic type qualifier to an incomplete type valid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM