Objective-C Function from C++

Nibbie
Posts: 1
Joined: 2010.07
Post: #1
Hi.

I'm new to objective-c and I'm trying to Mix C++ with objective-c. So far i have managed to call c++ classes and functions from obj-c. But I'm having trouble with the opposite.

So I want to call a function named bar in a obj-c class named Animal. I can use the varable foo from Animal.

The error i get is
Quote:struct Animal' has no member named 'bar'


cpp.cpp
Code:
#include "cpp.h"
#include <stdio.h>
#include "Animal.h"

void cpp::bar(int newFoo){
    foo = newFoo;
    
    Animal a = Animal();
    
    printf("foo: %i\n", a.bar(50) );
}

Animal.h
Code:
@interface Animal {
@public
    int foo;
}

- (int) bar: (int) input1;

@end


Animal.mm
Code:
#import "Animal.h"


@implementation Animal

- (int) bar: (int) input1
{
    return input1*2;
}

@end

Any ideas what is wrong?
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #2
Well, to be short, everything in cpp.cpp.

1) It shouldn't be .cpp, it should be .mm
2) "Animal" is an ObjC class, and so it is used like an ObjC class, using C++ object syntax on ObjC objects won't work at all. Instead of a.bar(50), use [a bar: 50]
3) Where is foo being defined in cpp.cpp (or where ever)?
4) Again, Animal a = Animal(); won't work, ObjC objects are ObjC objects, C++ structures and classes are something else. Use Animal* a=[[Animal alloc] init];


ObjC++ does no more magic than what the two languages do already, ObjC is used as ObjC, C++ is used as C++, the two object systems are incompatible and calling with the other's syntax is impossible.


Edit: Also, if you're trying to access foo from an instance of Animal (a), you have to explicitly do a->foo.
Quote this message in a reply
Member
Posts: 27
Joined: 2009.07
Post: #3
(Jul 24, 2010 10:19 AM)Oddity007 Wrote:  Edit: Also, if you're trying to access foo from an instance of Animal (a), you have to explicitly do a->foo.
Assuming you're referring to an "if the object was a C++ object" situation, it's only on pointers that you use the -> operator. Perhaps ObjC++ needs you to do that instead of a.foo on ObjC objects, I'm honestly not sure, but to my knowledge you still use dot syntax.

So, your cpp.cpp file needs to be renamed to cpp.mm, and here's how you'll want your code to look:
[code=objc]#include "cpp.h" // Honestly, I don't know what you have in here, so there may be more broken. I'm assuming, however, that you have defined the class as containing an integer member variable named "foo" and a function called bar.
#include <stdio.h>
#import "Animal.h"

void cpp::bar(int newFoo) {
foo = newFoo;

Animal* animal = [[Animal alloc] init];

printf("foo: %i\n", [a bar: 50]);

// FIXED. Never call dealloc on your own. Thanks for correcting me, Cookie. :o
[animal release]; // I may be new to ObjC/ObjC++, but I still hate automatic garbage collection. Rasp
}[/code]

Goodbye.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
You should *never* call dealloc yourself. That should be release.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #5
(Sep 23, 2010 06:54 AM)Minalien Wrote:  // I may be new to ObjC/ObjC++, but I still hate automatic garbage collection. Rasp

I'd just like to point out the small irony that most people (even those who are "good" at writing manual memory management code) hate manual memory management because no matter how much experience you have it's easy to make silly mistakes like this every now and then. Wink

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 27
Joined: 2009.07
Post: #6
(Sep 23, 2010 07:49 AM)OneSadCookie Wrote:  You should *never* call dealloc yourself. That should be release.
Always so hurtful, Cookie. Sad

Thanks for the correction - like I said, I haven't messed with ObjC/ObjC++ in ages, and even when I did it wasn't much. I'm not even going to bother touching ObjC again until XCode 4 is released, because I'm tired of fighting with XCode. xD

(Sep 23, 2010 08:48 AM)Skorche Wrote:  
(Sep 23, 2010 06:54 AM)Minalien Wrote:  // I may be new to ObjC/ObjC++, but I still hate automatic garbage collection. Rasp

I'd just like to point out the small irony that most people (even those who are "good" at writing manual memory management code) hate manual memory management because no matter how much experience you have it's easy to make silly mistakes like this every now and then. Wink
Yes, but you can't squeeze every inch of optimization and can actually cause a good number of problems by using automatic garbage collection. The only time I'm happy with using automatic memory management in an actual product (this excludes personal projects intended for experimentation and to facilitate learning) is when I've developed the memory management system myself. Of course, over-optimization is also one of my biggest problems when it comes to development. Rasp

Also, iDG needs a prettier syntax highlighter. D:

Goodbye.
Quote this message in a reply
Post Reply