[英]“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.