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.
#import
an .m
file into another file. You import the .h
file, if it's needed.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.I added a class that had exactly the same name as a class in the static library that I also used. So adding a prefix to the name of my class solved the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With