After hours of struggling, I have finally resolved this issue.
In case anyone is still suffering with this, I'll try to explain the reasons and the fix I've found.
- Don't let
USING_NS_CC; be declared in any of your header files.
- Don't include
UIKit in header. Move it into cpp.
- If you need to use a type from it, hide the implementation into a separate class.
Why is "Point" ambiguous in MacTypes.h?
There are two types of
Point types, one defined in Cocos2d and other by iOS. The problem rises because the compiler detects references to
Point type without specifying which one.
The C++ way is to use a "namespace", like
However, since it's cumbersome to write the prefix each time, there is a convention to declare "I'm going to omit the
cocos2d:: prefix in this scope. Just regard every
Point here as
cocos2d::Point.". That's the
using namespace cocos2d line, or even more convenient,
USING_NS_CC. This is causing the problem.
If there is any file that declared using namespace cocos2d in a header file, then it gets mixed up during the long include chain. Hence, "you shouldn't use
USING_NS_CC in header files".
So what should I do?
Simply remove all the
USING_NS_CC in every header files you have made, and use full type names like
Why is NSObjCRuntime.h complaining about parsing?
And here is another problem I've encountered after solving the problem. I don't know if it's only me, but I'll explain it anyway.
It complains that
NSObjCRuntime.h file (in iPhone SDK) doesn't understand any ObjC syntax and raises a Parse Issue. Something must have gone wrong here.
I found that you SHOULD NOT include Foundation.h at the header file.
A typical Objective C classes starts the header file by importing the
UIKit.h). However, this seems to collide with cocos2d-x, and you will have the errors above, perhaps along side with the MacTypes.h error discussed earlier.
Moving the include codes into the cpp implementation file seems to fix this.
By the way, it's fine to use
USING_NS_CC in cpp implementation files.
But I need to declare some iOS specific type!
What if you need to have some type at the class declaration, like a
In this case you need to hide the implementation from the header.
For what it's worth, you probably shouldn't declare iOS depdendant types in headers anyway, because it wouldn't work on other platforms. It should be an implementation detail, and is best kept inside only the cpp file.
You do this by creating a separate type -- a struct or class -- to hold the implementation detail code, and only have a pointer to the type. This is known as "Pointer to Implematation" Idiom, or simply PIMPL.
- In header, remove any
#import <UIKit/UIKit.h> line.
- If you need to use a specific type in class, create another implementation class and declare a pointer to it. You just do 'forward declaration' of the type in the header.
- In implementation, include the
#import <UIKit/UIKit.h> line.
- Create implementation class here. Now you can use
UIView types and such. You can declare the class interface in the cpp file as well, because it's an implementation detail.
Hope this helps anyone who's still having a hard time.