ERROR: Reference to "Point" is ambiguous in MacTypes.h

Hi everyone,
I’m trying to build my project, but i met the errors " Reference to “Point” is ambiguous in MacTypes.h
I use Cocos 2dx 3.0 and xCode 5.0.1 .
Do you have any idea about my problem ?
Thanks.

Cocos2dx 3.0 is not using CC- prefix anymore, so instead of CCPoint we have Point. It may conflict with Point declared in MacTypes.h.

I think you can type cocos2d:: as prefix for Point like

cocos2d::Point

to avoid this.

Thanks for your reply,
I tried to use “cocos2d::Point” but this error still occurs.
And i saw that it will occurs in “Pre-compile header files” and
xCode log " candidate found by name look up is “Point”
candidate found by name look up is " cocos2d::Point".

precompile ios/Prefix.pch.

#ifdef OBJC
#import < Foundation/Foundation.h >
#import < UIKit/UIKit.h >
#endif
That’s my Prefix.pch.

Do you have any idea about this ?

are you using any instance of Point in your code? or this happens even if you only build the test project? where (which files) are you adding cocos2d::prefix?

Yes, i used many instance of Point with cocos2d::Point in the file .cpp.
But it show more " reference to “Rect” is ambiguous", i don’t use any instance of “Rect”.

This happen only in 1 project, i test with new project that is fine.
I see in my xCode when it try to import “Foudation.h” -> The errors occurs.

I met the same issue,the engine’s version is v3.0Beta2

I’m getting the same errors - only when I include a particular header file.

it seems it has something to do with circular reference (i.e. A.h includes b.h and b.h includes A.h) but I’m too much oof a C++ noob to sort it out!

Hi everyone.

Same for me with v3.0 RC2 and XCode 5.1.1.
It only happens with the iOS target. Building for mac works fine. Any ideas?

Ok - this is what fixed it for me:

I had a class A that referenced a class B, and class B that referenced class A

In the .h files I removed #includes for A from B and B from A, and replaced them with class,

so
#include “A.h”
was replaced with
class A

I had to add the #include into A.cpp and b.cpp

it was actually far more complex than that - as I had a number of classes referring to a number of other classes, some of them base classes - but a bit of perseverance got it sorted.

I think the actual issue in my case was caused by class C #including A.h, which included B.h, which included some file somewhere which itself referred to a point without specifying the namespace - and using the various .h files now had two different namespaces to contend with - both of which contain definitions of point.

This subject is usually called ‘forward declaration’ in c++, when you declare just the name of a class instead of including it in the header, because, as pointed, when you have cross including, problem may arise.

There are a bunch of articles around that describe the situation, like:

Basically you can just declare class A instead of #include A every time you just need a reference to A in your file (A *myA_Instance) in your .h
Instead you NEED to #include A whenever you need to know the SIZE of what you are declaring (A myA_Instance) in your .h

Hope this helps :slight_smile:

Sorry for necromancing this topic, but this is the exact issue I am having. The problem is that the class im including libs in inherits from NSObject. So i NEED to include <Foundation/Foundation.h>. I can’t forward declaration, any help?

Sorry for what might seem a stupid reply:

  1. Did you try

    cocos2d::Point

  2. Which version of cocos2dx are you using? I had recently this problem with 3.2 and prefixing Point with cocos2d:: worked out.

Let me know if this helps! :slight_smile:

How to fix ambiguousness, we can change to cocos2d::Point or cocos2d::Rect in our project, but we can not change it in Cocos2d SDK

No one solve this yet. im using cocos2d 3.4 and i have this problem when i include https://github.com/hiepnd/Screw to my code :frowning: please any one can solve this .

Try to include “chipmunk.h” only in your cpp file and the “chipmunk.h” should be in the first line.

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:

  1. Don’t let USING_NS_CC; be declared in any of your header files.
  2. Don’t include Foundation or UIKit in header. Move it into cpp.
  3. 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 PIMPL.

  1. In header, remove any #import <UIKit/UIKit.h> line.
  2. 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.
  3. In implementation, include the #import <UIKit/UIKit.h> line.
  4. 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.

4 Likes

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!