簡體   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