Hi Mick,

The problem is that none of the functions here have prototypes, so the use of 'colorName' comes before the definition of the function 'colorName', which then has not matched the compiler's assumptions. Also, as given, the code spits out many warning about missing prototypes, which are the cause of the problem. Don't ignore warnings.

I'm not sure what "Learning Objective C on the Mac" is (a book?) if so, this code contains no Objective-C at all, is not object-oriented and apart from using a Cocoa class (NSString) in a not very clever or useful way, serves no real purpose as far as learning Objective-C is concerned (that I can see anyway). Perhaps it's given as a counter- example - how coding was done before Objective-C? Otherwise this looks like a text I'd steer clear of, especially if its code examples continue to be as sloppy as this one.

--Graham




On 27/09/2009, at 5:41 AM, Mick Walker wrote:

Hi,

I am currently working my way through Learn Objective C on the Mac, and have typed one of the code samples into Xcode (ver 3.2), however Xcode gives me a compile error, I think I have typed the code letter for letter, so I was just wondering if someone could tell me why the following code will not compile?

#import <Foundation/Foundation.h>

typedef enum {
        kCircle,
        kRectange,
        kOblateSpheroid
} ShapeType;

typedef enum {
        kRedColor,
        kGreenColor,
        kBlueColor
} ShapeColor;

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

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

        
int main (int argc, const char * argv[]) {

   Shape shapes[3];
        ShapeRect rect0 = {0, 0, 10, 30};
        shapes[0].type = kCircle;
        shapes[0].fillColor = kRedColor;
        shapes[0].bounds = rect0;
        
        ShapeRect rect1 = {30, 40, 50, 60};
        shapes[1].type = kRectange;
        shapes[1].fillColor = kGreenColor;
        shapes[1].bounds = rect1;
        
        ShapeRect rect2 = {15, 18, 37, 29};
        shapes[2].type = kOblateSpheroid;
        shapes[2].fillColor = kBlueColor;
        shapes[2].bounds = rect2;
        
        
        drawShapes(shapes, 3);
        
        return(0);
        
}// main

void drawShapes(Shape shapes[], int count){
        int i;
        for(i = 0; i< count; i++){
                switch(shapes[i].type){
                        case kCircle:
                                drawCircle(shapes[i].bounds, 
shapes[i].fillColor);
                                break;
                        case kRectange:
                                drawRectangle(shapes[i].bounds, 
shapes[i].fillColor);
                                break;
                        case kOblateSpheroid:
                                drawEgg(shapes[i].bounds, shapes[i].fillColor);
                                break;
                }
        }
} // drawShapes

void drawCircle (ShapeRect bounds, ShapeColor fillColor){
NSLog(@"drawing a circle at (%d %d %d %d) int %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
} // drawCircle
void drawRectangle (ShapeRect bounds, ShapeColor fillColor){
NSLog(@"drawing a rectangle at (%d %d %d %d) int %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
} // drawRectangle
void drawEgg (ShapeRect bounds, ShapeColor fillColor){
NSLog(@"drawing a egg at (%d %d %d %d) int %@", bounds.x, bounds.y, bounds.width, bounds.height, colorName(fillColor));
} // drawEgg

NSString* colorName(ShapeColor colName){
        switch (colName) {
                case kRedColor:
                        return @"red";                
                        break;
                case kGreenColor:
                        return @"green";
                        break;
                case kBlueColor:
                        return @"blue";
                        break;
        }
        return @"No clue";
}// colorName

The error i get is:
"Conflicting types for 'colorName'

Kind Regards
Mick Walker

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to