user1562021
user1562021

Reputation: 11

special character in #define macro

I am new to C++ and facing one easy problem mention below. in visual C++ 2008, i am trying to #define something like

#define fromThis*  toThisPtr
#define fromThis   toThisObj

I am porting some code that is written in linux and need to port in accordance to winapi. The error which i am getting something like this.

 error C2008: '*' : unexpected in macro definition
 warning C4005: 'fromThis' : macro redefinition
 see previous definition of 'fromThis'

I need to redefine fromThis* and fromThis during preprocessing time. Is special character in #define macro are not allowed? How can i work this code out?


EDIT-1: I am looking out possible solution to this. I am trying to repalce *fromThis to 'toThisPtr'. One suggested solution is use of typedefs. Which i did like below.

typedef toThisPtr   fromThis*;
typedef toThisObj   fromThis;

Now with this solution the error which i am getting is this:

error C2143: syntax error : missing ';' before '*' error C2059: syntax error : ';' error C2040: 'iovec' : 'toThisObj' differs in levels of indirection from 'toThisPtr'

can typedef be the siolution to this problem. What are the std way to replace fromThis* to change into toThisPtr?

Upvotes: 1

Views: 9200

Answers (4)

Ivan Kruglov
Ivan Kruglov

Reputation: 753

You can not use * in a name. See the excerpt from the documentation:

You may define any valid identifier as a macro, even if it is a C keyword

A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258648

You can't really do that, nor should you. :)

For this, I'd personally use typedefs instead of macros.

typedef toThisPtr fromThis*
typedef toThisObj fromThis

and then just do a replace all.

Upvotes: 1

jahhaj
jahhaj

Reputation: 3119

Perhaps you've just got it backwards

#define toThisPtr fromThis*
#define toThisObj fromThis

This defines two macros called toThisPtr and toThisObj. Otherwise I've having a really hard time understanding what you are trying to do.

Upvotes: 0

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263137

The documentation for #define says its first argument is an identifier. Identifiers can only contain letters, digits and underscores and must not start with a digit.

Therefore, fromThis* is not a valid identifier, and you cannot #define it to something else.

Upvotes: 3

Related Questions