Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > eeek! wierd errors in ObjC?

eeek! wierd errors in ObjC?
Thread Tools
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 7, 2001, 07:02 PM
 
Okay, i can't figure this out. Part of my program looks like this:

Code:
+ new: (KNode*) target { return [[self alloc] init: target]; } - init: (KNode*) target { ... // do stuff with target return self; }
The methods listed in the interface match exactly. I get an error on the line in 'new', claiming that init: has multiple definitions. It doesn't init: (ie with one argument) isn't used in any superclasses, or defined twice in the method body, or anything.

It's way too late and i don't get it. Any helpful thoughts?
All words are lies. Including these ones.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Mar 7, 2001, 08:19 PM
 
Objective-C doesn't offer that kind of polymorphism, unfortunately. init is usually written as
Code:
- init { if ( [super init] ) { ... (stuff) } return self; }
No arguments. Remember that this is object oriented programming, so doing something like

[knode init:knode] would be redundant. [knode init], using self for self-reference, is I think what you're looking for.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Mar 7, 2001, 08:29 PM
 
Are you getting an error or warning? When I tried your example, I got

foo.m:11: warning: cannot find method.
foo.m:11: warning: return type for `init:' defaults to id

which simply means the compiler hadn't seen a declaration for init:, which makes it assume it returns id. If you predeclare it, I don't get any warnings and it works.

ObjC does allow you to have init and init:. What it does not allow is having multiple methods with the exact same name (init:, init, which take different types of arguments (so no argument overloading).

Ali
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Mar 7, 2001, 08:33 PM
 
However, you still wouldn't want to init something passing it as the object.

Rather, you should send it a message, and have it reference itself.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
sadie  (op)
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 8, 2001, 02:11 AM
 
Thanks for the replies:

parrallax replied:
init is usually written as... No arguments

[knode init:knode] would be redundant. [knode init], using self for self-reference, is I think what you're looking for.
Sorry, i should have made it clearer. These aren't methods of KNode. The argument i'm using really is an argument that I need, not a way of referring to itself.


ali told me:
Are you getting an error or warning? When I tried your example, I got

foo.m:11: warning: cannot find method.
foo.m:11: warning: return type for `init:' defaults to id

which simply means the compiler hadn't seen a declaration for init:, which makes it assume it returns id. If you predeclare it, I don't get any warnings and it works.
That's exactly the error I'm getting! What do you mean, predeclare it? The exact same signature is in the @interface, in the header file, and i'm #importing it.

Code:
@interface ... + new: (KNode*) target; - init: (KNode*) target; @end
Isn't that enough?

ObjC does allow you to have init and init:. What it does not allow is having multiple methods with the exact same name (init:, init , which take different types of arguments (so no argument overloading).
I have many classes with their own init:, but not that inherit from each other. As far as i can tell, there are no clashes.



[This message has been edited by sadie (edited 03-08-2001).]
All words are lies. Including these ones.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Mar 8, 2001, 06:31 AM
 
Subclassing from NSObject, this compiles fine (exactly as your code says).

What are you subclassing from?
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
sadie  (op)
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 8, 2001, 09:06 AM
 
I'm inheriting from objc / Object rather than foundation / NSObject - i want it to be cross-platform, as it doesn't have a UI. It shouldn't make any difference - I've checked the interface for Object, and it doesn't use init: at any point that I can see.
All words are lies. Including these ones.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Mar 8, 2001, 09:33 AM
 
Directly from Object?

Code:
#include <objc/Object.h> typedef struct { int foo; } KNode; @interface Foo : Object { } + new: (KNode*) target; - init: (KNode *) target; @end @implementation Foo + new: (KNode*) target { return [[self alloc] init: target]; } - init: (KNode*) target { return self; } @end int main(int argc, char **argv) { }
cc main.m -lobjc

Works for me!
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
sadie  (op)
Senior User
Join Date: Feb 2001
Location: Rochester, uk
Status: Offline
Reply With Quote
Mar 8, 2001, 10:43 AM
 
I think i've got it!

It's a big project, and there's several different classes independently making their own - [init:] methods. None of them inherit from each other, except in safe ways, but some unrelated ones have different arguments.

One group may have:
- init: (KNode*) target;

another may have:
- init: (char*) name;

This would be fine if it weren't for this line:
[[self alloc] init: target];

The compiler doesn't know which sort of object is being returned from alloc , so it questions it. I've found that everything works if i do this:

[(myclass*) [self alloc] init: target];

I'm not sure if this is what you meant by 'that sort of polymorphism', but either way thanks for your help.

Any news on whether the GM will use gcc instead of cc?

All words are lies. Including these ones.
     
Admin Emeritus
Join Date: Oct 2000
Location: Boston, MA
Status: Offline
Reply With Quote
Mar 8, 2001, 11:56 AM
 
Aye, and you might want to rename your inits to something like initWithChar or initWithKNode just for clarity.
"Against stupidity, the gods themselves contend in vain" (Schiller)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:31 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2