重新定义一个没有按预期工作的宏
redefining a macro not working as expected
我在 C++ 中定义块,这些块位于特殊的内存区域。我想定义一个块,然后在为每个块重新定义的变量中定义下一个块的地址。
#include <iostream>
using namespace std;
#define BASE_ADDRESS 0X1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100)
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK
#define NEXT_FREE_BLOCK (TYPE_B + 200)
// ---end of RWW memory map---
int main()
{
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
return 0;
}
我查找的示例代码如下所示。但是当我尝试编译它时,我得到“由于以下错误编译失败”:
main.cpp:12:16: error: ‘NEXT_FREE_BLOCK’ was not declared in this scope
#define TYPE_B NEXT_FREE_BLOCK
^
main.cpp:14:26: note: in expansion of macro ‘TYPE_B’
#define NEXT_FREE_BLOCK (TYPE_B + 200)
^~~~~~
main.cpp:20:34: note: in expansion of macro ‘NEXT_FREE_BLOCK’
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
^~~~~~~~~~~~~~~
如果我注释掉 'type b' 行,它会按预期运行。如果我注释掉 #undef 行,它会告诉我我已经重新定义了宏。我想在不收到错误或警告的情况下重新定义宏;有办法吗?
让我们看一下这个例子:
#define MYMACRO 0 //MYMACRO = 0
#define ANOTHERMACRO MYMACRO //ANOTHERMACRO = MYMACRO = 0
int main() {
return ANOTHERMACRO;
}
还好吧?
但是如果我们这样做:
#define MYRECURSIVEMACRO 0 //MYRECURSIVEMACRO = 0
#define MYMACRO MYRECURSIVEMACRO //MYMACRO = MYRECURSIVEMACRO
#undef MYRECURSIVEMACRO // MYRECURSIVEMACRO does not exist
#define MYRECURSIVEMACRO (MYMACRO+10) //What was MYMACRO again? It was MYRECURSIVEMACRO, but now we are setting MYRECURSIVEMACRO, which right now is being defined, to itself plus 10, and now I am 100% confused
#define ANOTHERMACRO MYRECURSIVEMACRO //Now what?
int main() {
return ANOTHERMACRO;
}
我们得到这个:
1>C:\dev\Stack Overflow\Source.cpp(520,9): error C2065: 'MYMACRO': undeclared identifier
(至少对于 MSVC++)
现在,如果我们看这个:
#define BASE_ADDRESS 0X1000 //BASE_ADDRESS = 0x1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS //TYPE_A = BASE_ADDRESS = 0x1000
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100) //NEXT_FREE_BLOCK = 0x1000 + 100
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK //TYPE_B = NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK //NEXT_FREE_BLOCK is gone
#define NEXT_FREE_BLOCK (TYPE_B + 200) //Now what? Same problem as before!
// ---end of RWW memory map---
这就是它不起作用的原因。
我在 C++ 中定义块,这些块位于特殊的内存区域。我想定义一个块,然后在为每个块重新定义的变量中定义下一个块的地址。
#include <iostream>
using namespace std;
#define BASE_ADDRESS 0X1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100)
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK
#define NEXT_FREE_BLOCK (TYPE_B + 200)
// ---end of RWW memory map---
int main()
{
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
return 0;
}
我查找的示例代码如下所示。但是当我尝试编译它时,我得到“由于以下错误编译失败”:
main.cpp:12:16: error: ‘NEXT_FREE_BLOCK’ was not declared in this scope
#define TYPE_B NEXT_FREE_BLOCK
^
main.cpp:14:26: note: in expansion of macro ‘TYPE_B’
#define NEXT_FREE_BLOCK (TYPE_B + 200)
^~~~~~
main.cpp:20:34: note: in expansion of macro ‘NEXT_FREE_BLOCK’
cout<<"Hello free block " << NEXT_FREE_BLOCK << endl;
^~~~~~~~~~~~~~~
如果我注释掉 'type b' 行,它会按预期运行。如果我注释掉 #undef 行,它会告诉我我已经重新定义了宏。我想在不收到错误或警告的情况下重新定义宏;有办法吗?
让我们看一下这个例子:
#define MYMACRO 0 //MYMACRO = 0
#define ANOTHERMACRO MYMACRO //ANOTHERMACRO = MYMACRO = 0
int main() {
return ANOTHERMACRO;
}
还好吧?
但是如果我们这样做:
#define MYRECURSIVEMACRO 0 //MYRECURSIVEMACRO = 0
#define MYMACRO MYRECURSIVEMACRO //MYMACRO = MYRECURSIVEMACRO
#undef MYRECURSIVEMACRO // MYRECURSIVEMACRO does not exist
#define MYRECURSIVEMACRO (MYMACRO+10) //What was MYMACRO again? It was MYRECURSIVEMACRO, but now we are setting MYRECURSIVEMACRO, which right now is being defined, to itself plus 10, and now I am 100% confused
#define ANOTHERMACRO MYRECURSIVEMACRO //Now what?
int main() {
return ANOTHERMACRO;
}
我们得到这个:
1>C:\dev\Stack Overflow\Source.cpp(520,9): error C2065: 'MYMACRO': undeclared identifier
(至少对于 MSVC++)
现在,如果我们看这个:
#define BASE_ADDRESS 0X1000 //BASE_ADDRESS = 0x1000
// type a gets 100 bytes
#define TYPE_A BASE_ADDRESS //TYPE_A = BASE_ADDRESS = 0x1000
#define NEXT_FREE_BLOCK (BASE_ADDRESS + 100) //NEXT_FREE_BLOCK = 0x1000 + 100
// type b gets 200 bytes, starting at the next free address
#define TYPE_B NEXT_FREE_BLOCK //TYPE_B = NEXT_FREE_BLOCK
#undef NEXT_FREE_BLOCK //NEXT_FREE_BLOCK is gone
#define NEXT_FREE_BLOCK (TYPE_B + 200) //Now what? Same problem as before!
// ---end of RWW memory map---
这就是它不起作用的原因。