2013-10-17 47 views
1
#include<vector> 
#include<stdint.h> 
#define RAM_M_V_INSERT_T32(vec,Long,pos) \ 
vec.at(pos)=(((tU8)((Long) >> 24)) & 0xFF); \ 
pos++;\ 
vec.at(pos)=(((tU8)((Long) >> 16)) & 0xFF); \ 
pos++;\ 
vec.at(pos)=(((tU8)((Long) >> 8)) & 0xFF); \ 
pos++;\ 
vec.at(pos)=(((tU8)((Long))) & 0xFF); 

int main() 
{ 
std::vector<char> c8vBuf; 
c8vBuf.at(0)=(char)SYSTEM_U32_SHUTDOWN_CPU_WATCHDOG; 
RAM_M_V_INSERT_T32(c8vBuf, (_u32WdtCount - 1),1); 
RAM_M_V_INSERT_T32(c8vBuf, _u32WdtCount,5); 
return 0; 
} 

當我嘗試編譯的問題,我得到這個錯誤,有關遞增操作數宏給出了GCC

cstr.cpp:19:3: error: lvalue required as increment operand 
cstr.cpp:19:3: error: lvalue required as increment operand 
cstr.cpp:19:3: error: lvalue required as increment operand 
cstr.cpp:20:3: error: lvalue required as increment operand 
cstr.cpp:20:3: error: lvalue required as increment operand 
cstr.cpp:20:3: error: lvalue required as increment operand 

請人闡明這光???

+3

你期望1 ++和5 ++能做什麼? – chris

+0

是否有一個允許使用宏的編譯器,或者是您使用GCC的事實只是特定的編譯器,而您沒有在其他編譯器上測試代碼? –

+0

您可以(也應該)運行您的編譯器來喚醒預處理器,並檢查生成的代碼。它可以啓發。 – ChuckCottrill

回答

3

宏將基本上執行通過預處理器發生的文本替換。

該宏會將您的代碼變成類似1++5++的東西。這些是整數文字,這意味着編譯器將它們標記爲「純」rvalues(prvalues)。 prvalues與l值不同

2

鑑於您將++運算符應用於參數pos,您不能將類似1或5的常量作爲第三個參數傳遞給宏。任何讓你這樣做的編譯器都是錯誤的。