-1

这是Circle.h

#import <Foundation/Foundation.h>

typedef enum{
    KCircle,
    KRectangle
}ShapeType;

typedef enum{
    KRedColor,
    KGreenColor,
    KBlueColor
}ShapeColor;

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


typedef struct{
    ShapeType type;
    ShapeColor fillColor;
    ShapeRect bounds;
}Shape;

@interface Circle : NSObject{
    ShapeColor mColor;
    ShapeRect mBound;
}

-(void) setBound: (ShapeRect) bound;
-(void) setFillColor: (ShapeColor) color;
-(void) draw;
@end

这是Circle.m

#import "Circle.h"

@implementation Circle

-(void) setBound:(ShapeRect)bound{
    mBound = bound;
}

-(void)setFillColor:(ShapeColor)color{
    mColor = color;
}

-(NSString*) colorName: (ShapeColor)color{
    switch (color) {
        case KGreenColor:
            return @"Green";
            break;
        case KRedColor:
            return @"Red";
            break;
        case KBlueColor:
            return @"Blue";
            break;
        default:
            break;
    }
}

-(void) draw
{
    NSLog(@"drawing a circle (%d %d %d %d) in %@",
          mBound.x, mBound.y, mBound.width, mBound.height, colorName(mColor));
}

@end

当我编译文件时,我遇到了这个错误:

Undefined symbols for architecture x86_64:
"_colorName", referenced from:
  -[Circle draw] in Circle.o
 (maybe you meant: _colorName1)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
4

1 回答 1

1
-(NSString*) colorName: (ShapeColor)color;

是一个 Objective-C方法而不是一个函数,所以你必须使用正确的语法来调用方法。代替

colorName(mColor)

经过

[self colorName:mColor]
于 2013-09-02T13:30:45.427 回答