views:

227

answers:

1

operator class:

#import <Foundation/Foundation.h>


@interface operator : NSObject {

int number;
}

@property int number;

@end

@implementation operator

- (id)init{
    self = [super init];
    if (self) {
    [self setNumber:0];
    }
    return self;
}

@synthesize number;
@end

main.m:

#import <UIKit/UIKit.h>
#import "operator.m"

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

id operator1 = [[operator alloc] init];
id operator2 = [[operator alloc] init];

[operator1 setNumber:10];
[operator2 setNumber:20];

int answer = [operator1 number] + [operator2 number];

printf("The answer is %d",answer);



NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}

I get an error -> ld: duplicate symbol _OBJC_IVAR_$_operator.number in /Volumes/Home/Desktop/testing/build/testing.build/Debug-iphonesimulator/testing.build/Objects-normal/i386/operator.o and /Volumes/Home/Desktop/testing/build/testing.build/Debug-iphonesimulator/testing.build/Objects-normal/i386/main.o

This is my very first time I program in ObjC. Am I doing something wrong?

I tried the "Clean all targets" fix that I found on google but did not help.

+3  A: 
  1. You should never #import an .m file into another file. You import the .h file, if it's needed.
  2. You should not have code executing in main before you create the autorelease pool. That is going to cause problems sooner or later. In this case, you test code should probably go in application:didFininshLaunching instead.
calmh
@alexBrand: You're combining the @interface and @implementation for operator in the same file, and then importing the whole thing. Don't do that. Put them in operator.h and operator.m, and then #import only operator.h into main.m.
quixoto
@calmh I imported the operator.h file and it works :). Thanks a lot. I also moved the autorelease pool to the beginning of the code.Now, how do I go about using the application:didFinishLaunching?
alexBrand