这是我在一本教程上面照搬下来的代码,一直编译不过去(OS X El Capitan Public Beta 5/ Xcode 7 beta 6),代码如下:
#import
typedef enum {circle,rectangle,egg} shapeType;
typedef enum {red,green,blue} shapeColour;
typedef struct {int x,y,width,height;} shapeRect;
typedef struct {shapeType type; shapeColour colour; shapeRect bounds;} Shape;
int main(int argc, const char *argv[])
{
Shape shapes[3];
shapeRect rect0={0,0,10,30};
shapes[0].type=circle;
shapes[0].colour=red;
shapes[0].bounds=rect0;
shapeRect rect1={30,40,50,60};
shapes[1].type=rectangle;
shapes[1].colour=green;
shapes[1].bounds=rect1;
shapeRect rect2={15,18,37,29};
shapes[2].type=egg;
shapes[2].colour=blue;
shapes[2].bounds=rect2;
drawShapes(shapes,3);
return(0);
}
void drawShapes(Shape shapes[],int count)
{
for (int i=0; i<count; i++) {
switch (shapes[i].type) {
case circle:
drawCircle(shapes[i].bounds,shapes[i].colour);
break;
case rectangle:
drawRectangle(shapes[i].bounds,shapes[i].colour);
break;
case egg:
drawEgg(shapes[i].bounds,shapes[i].colour);
break;
}
}
}
void drawCirle(shapeRect bounds,shapeColour colour)
{
NSLog(@"Drawing a circle at (%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colourName(colour));
}
void drawRectangle(shapeRect bounds,shapeColour colour)
{
NSLog(@"Drawing a rectangle at (%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colourName(colour));
}
void drawEgg(shapeRect bounds,shapeColour colour)
{
NSLog(@"Drawing an egg at (%d %d %d %d) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colourName(colour));
}
NSString *colourName(shapeColour colourName)
{
switch (colourName) {
case red:
return @"red";
break;
case green:
return @"green";
case blue:
return @"blue";
}
return @"no clue";
}
报错如下:
看图,在主函数前面需要先定义要使用的函数,不然编译器就找不到要使用的方法。
PS:方法drawCircle中单词Circle写错成Cirle了