开发者

Incompatible type for argument 1 of 'setBounds'

I am trying to do my own custom classes and learn C and Objective C. I'm recieving the error that there is an incompatible type for argument 1. I've defined a struct and class like this:

typedef enum {
 kRedColor,
 kGreenColor,
 kBlueColor
} ShapeColor;


typedef struct {
 int x, y, width, height;
} ShapeRect;


@interface Shape : NSObject
{
 ShapeColor  fillColor;
 ShapeRect   bounds;
}

- (void) setFillColor: (ShapeColor) fillColor;
- (void) setBounds: (ShapeRect) bounds;
- (void) draw;

@end // Shape

Then I import the Shape.h file(code above) and try and开发者_运维知识库 create a shape like this:

id shapes[4]; // I'm different!

ShapeRect rect0 = { 0, 0, 10, 30 }; shapes[0] = [Shape new]; [shapes[0] setBounds: rect0];

I get the error that setBounds is incompatible. For some reason it isn't looking at the Shape.h class for the setBounds method and it is instead looking at the default setBounds method? Is there something I'm doing wrong? Thanks!


If there is another method called setBounds: then using type id will usually result in the compiler picking the first encountered setBounds: (for determining return types and argument types), and since yours probably not the first, it is giving the error. You either need to tell the compiler that you want to use your setBounds: by changing the type from id to Shape *, but you can also cast your id to a Shape * and it should work also:

[(Shape *)shapes[0] setBounds:rect0];


With your code, shapes[0] is just an id, for which the compile does not know there is setBounds:. Instead, you should declare shapes as

Shape* shapes[4];

By the way, if you had an error, please post exactly what error was spit out by the compiler, not just saying '... was incompatible', because there are many ways in which a thing can be incompatible! Writing it down explicitly would help people here answering your question, because that way we don't have to guess exactly what has happened. Eventually, you yourself would become able to understand from the error message what is going wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜