簡體   English   中英

C:連續宏不起作用

[英]C: Consecutive macros don't work

我有一個在屏幕上顯示字符串的函數。 原型是dispStrFont(char* str, struct Font font, int x, int y, int color)

但是,由於鍵入字體很煩人,所以我創建了一個宏#define dispStr(str, x, y, color) dispStrFont(str, normal_font, x, y, color) 編譯時似乎工作正常(無錯誤)。

我的大多數琴弦都是黑色的,因此不需要輸入顏色。 因此,我通過另一個宏(放置在上述宏之前)使顏色#define dispStr(str, x, y) dispStr(str, x, y, 0)可選: #define dispStr(str, x, y) dispStr(str, x, y, 0)

這兩個宏的組合會產生錯誤,我不知道為什么。

頭文件:

#define dispStr(str, x, y) dispStr(str, x, y, 0)
#define dispStr(str, x, y, color) dispStrFont(str, normal_font, x, y, color)
//the define above gives me a warning: "Incompatible redefinition of macro "dispStr" (declared at line 1)"

主文件:

dispStr("test", x, y) //gives me an error, saying there's an illegal token ')'
                      //also gives me a warning "Too few arguments in macro invocation"
dispStr("test", x, y, 0) //compiles fine

為什么會這樣呢? 另外,當我注釋第二個定義時,它不會給我括號錯誤(但由於無法識別dispStr函數而無法編譯),因此它是對dispStr(str, x, y)的連續定義) dispStr(str, x, y) ,導致錯誤。

編輯:最終修改宏以刪除不必要的組合。 因此, define dispStr(str, x, y) dispStr(str, x, y, 0)成為define dispStr(str, x, y) dispStrFont(str, normal_font, x, y, 0) 我還必須將該定義放在另一個之后,否則由於某種原因它仍然給我括號錯誤。

您不能重載宏。 此外,如果已經調用了宏,則不會再次調用它。

您應該給宏命名不同的名稱。 您也可以使用GNU擴展以及C99及更高版本支持的可變參數宏

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM