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