Re: Compile error about MACRO
- From: Norbert Unterberg <nunterberg@xxxxxxxxxxxxxxxxx>
- Date: Mon, 26 Nov 2007 08:27:58 +0100
George schrieb:
The compile error of the following code is,
foo.cpp(7) : error C2146: syntax error : missing ';' before identifier '_'
foo.cpp(7) : error C2065: '_' : undeclared identifier
foo.cpp(7) : error C2143: syntax error : missing ';' before 'string'
I do not know why there is errors. I think in MERGE macro, the value of parameter a is hello and the value of parameter b is world in my case, and I defined hello_world to "H E L L O", it should be fine...
Wrong. In the MERGE macro, a is replaced by the string "hello", not the token hello. You only use quotes in macro arguments when you want strings.
Any ideas?
[Code]
#define MERGE(a,b) a ## _ ## b
#define hello_world "H E L L O"
int foo()
{
char* p0;
p0 = MERGE("hello", "world");
This expands to:
p0 = "Hello"_"world";
which is definately a syntax error. Try:
p0 = MERGE(hello, world);
You are just experiencing the difference between a string and a token. The ## operator works on tokens not on strings. To merge strings you do not need the ## operator.
Norbert
.
- Follow-Ups:
- Re: Compile error about MACRO
- From: George
- Re: Compile error about MACRO
- Prev by Date: Re: Make service unstoppable to protect from unauthorized user
- Next by Date: sending input to window of another application
- Previous by thread: Mr. spaAtse3bzLivUcyrlW7lh, Thank you for being so manic.
- Next by thread: Re: Compile error about MACRO
- Index(es):
Relevant Pages
|