views:

36

answers:

2

This is my first ever attempt at a Objective-c cocoa program, so I have no clue why it is giving me that error. I really don't understand the Build Result page either.

myClass.h

#import <Cocoa/Cocoa.h>


@interface myClass : NSObject {
    int a;
    int b;
}

-(void) setvara:(int)x;
-(void) setvarb:(int)y;
-(int) add;

@end

myClass.m

#import "myClass.h"


@implementation myClass

-(void)setvara:(int)x{
    a=x;    
}

-(void)setvarb:(int)y{
    b=y;    
}

-(int)add{
    return a+b; 
}
@end

main.m

#import <Cocoa/Cocoa.h>
#import <stdio.h>
#import "myClass.m"

int main(int argc, const char* argv[])
{
    myClass* class = [[myClass alloc]init];

    [class setvara:5];
    [class setvarb:6];

    printf("The sum: %d", [class add]);

    [class release];

}
+4  A: 

In your main.m, you want to import myClass.h, not myClass.m

The header file has the declarations you need. If you import the implementation, you are implementing those methods twice, hence the duplicate symbols.

Another tip as you learn, when you say [[myClass alloc] init], what you get back is a pointer to an object, not a class. So you should call it an object just so that concept is reinforced for you. Getting the difference straight now will help you greatly as you get deeper into this.

(there are a couple of naming convention issues here also, btw)

Firoze Lafeer
Ok so it is a class but it creates an object?
Adam
yes, you send an 'alloc' message to a class, and what you get back is a pointer to an object. Then you send an 'init' message to that object, and you get back a pointer to an object that is now initialized and ready to use.
Firoze Lafeer
A: 

Hey i tried your code and i found that in the main.m file you are importing the myclass.m file it should be myclass.h apart from this issue your code just works fine man.

Radix