繁体   English   中英

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

[英]“useless type qualifier” error

我使用以下代码得到以下错误。 我试图找出问题在谷歌的哪个方面,但我没有找到任何有用的信息。

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]

这是touchpad.h第12行到第17行的代码:

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

以下是我在touchpad.c使用此结构的方法:

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

有谁能告诉我光明? :d

volatile作为限定符可以应用于特定的结构实例。
您正在将它应用于无用的类型,并且编译器正确指出它。

volatile限定变量,而不是类型。

这样做:

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

是合法的,因为:

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

static volatile struct cal cal;

volatile关键字对于对象是有意义的。 不是类型定义。

你不会得到错误,只是一个警告。

这适用于你如何声明你的struct cal :它本身并不易变; volatile仅适用于具体的变量定义。

所以在static struct cal cal ,你的变量cal只是static ,但不是volatile

从这个意义上说,正如警告所说, volatile声明是无用的。

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

您不能将volatile限定符附加到struct声明。

但是,与其他答案相反,您实际上可以在类型上使用volatile限定符,但不在 结构上使用 如果使用typedef ,则可以为结构创建volatile类型。

在下面的示例中, Vol_struct实际上不是易失性的,如海报的问题所示。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM