views:

110

answers:

2

The function NSStringFromPoint disappears when I compile my code using objective-C++, but it's fine under objective-C.

How can I make objective-C++ see that function?

If I compile under Objective-C++ it says:

error: 'NSStringFromPoint' was not declared in this scope
error: 'NSStringFromRect' was not declared in this scope
error: 'NSEqualSizes' was not declared in this scope
+1  A: 

I compiled this simple application:

#include <Cocoa/Cocoa.h>
int main (void)
{
    NSLog (@"%@", NSStringFromPoint(NSMakePoint(10, 10));
    return 0;
}

Using this command line:

gcc -x objective-c++ test.mm -framework Cocoa -lstdc++

And I got this output (ignoring the error about no autorelease pool in place):

2010-05-12 12:41:33.946 a.out[290:10b] {10, 10}

Make sure you're including the right headers, at the very least, make sure you're importing <Foundation/Foundation.h>. An explicit #import <Foundation/Foundation.h> will do no harm if it has already been included.

dreamlax
As I stated, it works fine under objective-C. All the relevant (and irrelevant) headers are included
John Smith
Well, @John, you have a couple of options since it's working fine elsewhere, just not in your environment. Are you using the same command line options as dreamlax? If you do _exactly_ what dreamlax did, does it work?
paxdiablo
@dreamlax, why sometimes include and sometimes import in Obj-C? Is there a difference?
paxdiablo
Also, I'm working with Foundation and UIKit (iphone programming) with the IDE. So the simple example is mostly irrelevant.
John Smith
@paxdiablo: It makes no difference in this case. `#import` in Objective-C just provides automatic include guards (or something along those lines, it just ensures that the header is only included once).
dreamlax
@John Smith: Perhaps appropriately tagging your question would give you more relevant results. There is nothing in your question or tags to indicate that this was the case.
dreamlax
@John Smith: Also, when I don't include/import `<Foundation/Foundation.h>` I get exactly the same error you do, except it says that `NSLog`, `NSMakePoint` and `NSStringFromPoint` were not declared in this scope.
dreamlax
+2  A: 

Someone can correct me if I'm wrong, but if you're linking against the iPhone SDK, there is no NSPoint or NSStringFromPoint. UIKit uses the Core Graphics structs CGPoint, CGSize and CGRect. The equivalent function would be NSStringFromCGPoint.

The Simulator libraries do not quite match up with the iPhone libraries -- I'm fairly certain applications compiled for the simulator link against the Mac's own Foundation.framework. For example, I wasted a lot of time in the pre-2.0 days thinking that NSXMLDocument was available on iPhone because it compiled and ran in the simulator.

Alex
With your hint I was able to build the full translation dictionary from NS to CG.
John Smith
Since `NSPoint` and `CGPoint` have the same layout, you can convert one to the other quite easily by doing this: `CGPoint cgPoint = *(CGPoint *)`
Alex
Or use NSPointToCGPoint (or From for the other direction), or define `NS_BUILD_32_LIKE_64` (or build 64-bit), which will make NSPoint and CGPoint exactly equal, so that no explicit conversion of any form is necessary.
Peter Hosey