Re: Compile error about MACRO



Thanks Norbert,


I am using Visual Studio 2005, are there any tools or compiler option which
could see the preprocessor processing result of a source file?


regards,
George

"Norbert Unterberg" wrote:

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


.



Relevant Pages

  • Re: Compile error about MACRO
    ... George schrieb: ... 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... ... You only use quotes in macro arguments when you want strings. ... The ## operator works on tokens not on strings. ...
    (microsoft.public.vc.language)
  • Re: Using Japanese and English strings, encodings
    ... using edict + CLISP + araneida. ... It would be much better if you had a single LANG macro, ... It keeps screwing up the EUC-JP encoding of any parameters I ... Internally, strings are 16 bit characters, I think. ...
    (comp.lang.lisp)
  • Re: Please, please, please Help !!!
    ... I have a set of 100,000 ascii strings, up to 255 chars each. ... Each string has 1 or more words (tokens), ... if I have strings in: mary poppins, brad pitt, yygr ...
    (microsoft.public.sqlserver.xml)
  • Re: override locale computer settings
    ... So you could make that macro run each time you open the workbook--just name it ... If you put those strings on a separate worksheet, ... I do not want to have the the format of the region of the user, ...
    (microsoft.public.excel.misc)
  • Re: Stupid Newbie Needs Help
    ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
    (comp.lang.c)