##cocos2dx - Reference to 'Point' is ambiguous
cocos2dx - Reference to 'Point' is ambiguous
原因:
Showing Recent Messages
/Users/staff/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.0.sdk/usr/include/MacTypes.h:538:8: Candidate found by name lookup is 'Point'
/Users/staff/Documents/cocos-quick/cocos-quick-cpp/publibs/cocos2dx/cocos/math/Vec2.h:760:14: Candidate found by name lookup is 'cocos2d::Point'
就是找到了两个Point的定义,无法区分是那个,引起歧义。
解决:
一、
#意思就是有歧义,一般在cpp这种情况下出现这种情况
#include "cocos2d.h" //include cocos2d 的一个文件
#import <StoreKit/StoreKit.h> // import 一个系统文件
如果是这种顺序导致的, 那么把 include 放到 import 后面去。
http://www.cocoachina.com/bbs/read.php?tid-334003-fpage-2.html
二、
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.
In short:
Don't let USING_NS_CC; be declared in any of your header files.
Don't include Foundation or 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 cocos2d::Point.
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 cocos2d::Point.
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 Foundation.h (or 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 UIView variable?
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 PIMPL6.
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.
jangxyz has the correct answer. I spent a while pondering how it was that a namespaced type like cocos2d::Point could be interfering with a non-namespaced Point class until I read his analysis about the whole "using namespace" bit. That's what makes the type ambiguous in that specific context. By removing that USING_NS_CC everything went perfectly!
大致意思就是,不要使用using namespace xx;
使用包含命名空间的变量类型
http://discuss.cocos2d-x.org/t/error-reference-to-point-is-ambiguous-in-mactypes-h/11853/18
浙公网安备 33010602011771号