Re: Compile error: "multiple methods named '-initWithSize:' found"
Re: Compile error: "multiple methods named '-initWithSize:' found"
- Subject: Re: Compile error: "multiple methods named '-initWithSize:' found"
- From: Julian Wood <email@hidden>
- Date: Fri, 11 Dec 2009 11:56:45 -0700
Thanks Bill, that explains it.
Another simple way to fix the issue was to cast it.
... = [(Square *)[Square alloc] initWithSize: ...];
But I think it is probably better, as you said, to come up with an init method name that is more descriptive, rather than reuse method names from other classes with different argumentation.
Cheers,
J
On 2009-12-09, at 1:01 AM, Bill Bumgarner wrote:
>
> On Dec 8, 2009, at 11:24 PM, Julian Wood wrote:
>
>> main.m: In function 'main':
>> main.m:7: warning: multiple methods named '-initWithSize:' found
>> /System/Library/Frameworks/AppKit.framework/Headers/NSImage.h:81: warning: using '-(id)initWithSize:(NSSize)aSize'
>> Square.h:4: warning: also found '-(Square *)initWithSize:(int)s'
>> main.m:7: error: incompatible type for argument 1 of 'initWithSize:'
>>
>> Why would those two methods clash? NSImage is not even included. Moreover, aren't we saying specifically to use initWithSize from Square? And finally, why does it think we are not using an int as a param?
>
> More likely than not, <Cocoa/Cocoa.h> is in your precompiled header or otherwise included.
>
> The two methods class because the argumentation is different. Though you didn't paste it, I'd bet you have this line of code:
>
> ... = [[Square alloc] initWithSize: ...];
>
> If you look at the declaration of +alloc, it returns (id) -- a reference to an instance of any class. Thus, when the compiler compiles the -initWithSize: call, it is [correctly] warning that it there might possibly be ambiguity. In your case, the ambiguity may cause failures in that passing a struct and passing an (int) might require a different ABI -- a different layout of parameters on the stack and in registers depending on architecture.
>
> The way to fix this is to never name a method the same as another method, but have different argument types. Not ever. If you were to poke through the Cocoa APIs, you'd find that, save for a very very very few cases that are all bugs, the Mac OS X APIs avoid this issue.
>
> It is also useful to avoid the use of (id) as much as possible. It isn't entirely unavoidable in that Objective-C doesn't support covariant declarations of methods. For example, the +array method might return an NSArray or an NSMutableArray [conceptually anyway as the actual implementation returns something completely different] depending on which class's +array implementation you invoke. Because Objective-C can't deal with:
>
> + (NSArray *) array;
>
> ... and this in a subclass ...
>
> + (NSMutableArrayy *) array;
>
> ... the +array method is declared as returning the generic (id) so as to avoid required typecasting or annoying warnings.
>
> (Yes, there is a bug tracking this particular issue.)
>
> b.bum
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden