0

i have problem to set in to c++ MACRO singletone function result . this is what i have :

the macro

#define CCDICT_FOREACH(__dict__, __el__) \
    CCDictElement* pTmp##__dict__##__el__ = NULL; \
    if (__dict__) \
    HASH_ITER(hh, (__dict__)->m_pElements, __el__, pTmp##__dict__##__el__)

and this is how i try to set it :

CCDictElement* pElement = NULL;
CCDICT_FOREACH(GameSingleTone::getInstance()->getGemsDictionary(), pElement)
{
}

the method getGemsDictionary() returns me: CCDictionary*,gemsDictionary;

the compilation error im getting is (on the line of the MACRO):

error C2143: syntax error : missing ';' before '{'

but if i do :

CCDictionary* tempDictionary = CCDictionary::create();
tempDictionary = GameSingleTone::getInstance()->getGemsDictionary();
CCDICT_FOREACH(tempDictionary , pElement)
{
}

every thing is working .
why ?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • You're using [reserved identifiers](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Also, macros are text substitution. View the post-preprocessor output from your compiler for the first. – chris Aug 25 '13 at 13:11
  • 3
    `pTmpGameSingleTone::getInstance()->getGemsDictionary()pElement` is not a valid identifier. – Benjamin Lindley Aug 25 '13 at 13:13

1 Answers1

2

Macros simply do text replacement. So when you do this:

CCDICT_FOREACH(GameSingleTone::getInstance()->getGemsDictionary(), pElement)

This line:

CCDictElement* pTmp##__dict__##__el__ = NULL; \

becomes this:

CCDictElement* pTmpGameSingleTone::getInstance()->getGemsDictionary()pElement = NULL;

Which is utter nonsense. This, on the other hand:

CCDICT_FOREACH(tempDictionary , pElement)

translates to this:

CCDictElement* pTmptempDictionarypElement = NULL;

Which is perfectly okay.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274