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;
}
你到底在做什麼?這可能比宏觀濫用更好。 – Mysticial 2012-03-01 21:01:49
我正在嘗試跟蹤數字abc。然後在編譯時使用它。 – 2012-03-01 21:04:46
@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