2012-03-01 99 views
0

我想在預處理器中創建一個可以增加的「全局變量」。宏重定義

例如,我已將abc定義爲1。下次我可以重新定義它爲2(當我這樣做時,我有重新定義錯誤)?我需要先使用undef嗎?但是 使用undef時出現編譯錯誤。

做這樣的事情的正確方法是什麼?

[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o 

error: use of undeclared identifier 'BOOST_PP_INC_abc' 

    std::cout << temp << endl; 

note: instantiated from: 

    #define temp BOOST_PP_INC(abc) 

note: instantiated from: 

    #define BOOST_PP_INC(x) BOOST_PP_INC_I(x) 
note: instantiated from: 

    #define BOOST_PP_INC_I(x) BOOST_PP_INC_ ## x 

<scratch space>:150:1: note: instantiated from: BOOST_PP_INC_abc 

1 error generated. 
make[2]: *** [CMakeFiles/main.dir/main.cpp.o] Error 1 
make[1]: *** [CMakeFiles/main.dir/all] Error 2 
make: *** [all] Error 2 

下面是代碼

#include <iostream> 
#include <boost/preprocessor/slot/counter.hpp> 
#include <boost/preprocessor/arithmetic/add.hpp> 
using namespace std; 

int main() { 
    std::cout << "Hello" << std::endl; 

    #define abc 1 
    #define temp BOOST_PP_INC(abc) 
    #undef abc 

    std::cout << temp << endl; 
    return 0; 
} 
+0

你到底在做什麼?這可能比宏觀濫用更好。 – Mysticial 2012-03-01 21:01:49

+0

我正在嘗試跟蹤數字abc。然後在編譯時使用它。 – 2012-03-01 21:04:46

+0

@NegativeZero,使用cog:http://nedbatchelder.com/code/cog/批次清理器可以處理這個宏觀混亂。檢查此答案查看可能與您相關的示例http://stackoverflow.com/questions/2506167/c-macros-with-memory/9455483#9455483 – lurscher 2012-03-01 21:28:34

回答

2

不能更改預處理器恐怕裏面預處理器宏的值。也許你應該重新考慮你想要完成的基本目標?您尚未說明增加預處理器值的基本目的是什麼。