1

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];

}
5
  • Try importing <Foundation/NSObject.h> and forget the cocoa.h Commented Oct 23, 2010 at 2:26
  • when you get a failed compilation with exit code 1, usually you can go to the build results and then click on the little button with the few horizontal lines. this will give you some compiler output that may be helpful to trackdown your problem. Commented Oct 23, 2010 at 2:33
  • Thanks. Still is throwing me the error. Commented Oct 23, 2010 at 2:35
  • Says duplicate symbol... I was reading something about Targets... do I have to set something up when compiling projects? I just want to use the console for printfs Commented Oct 23, 2010 at 2:37
  • thyrgle: One should generally not import specific classes' headers directly. The proper way is to import the framework's umbrella header. Commented Oct 23, 2010 at 2:48

1 Answer 1

4

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Ok so it is a class but it creates an object?
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.