作者:潇潇雨621715 | 来源:互联网 | 2023-09-23 11:34
最近看《Objective-C 程序设计》 ,11章有个合成对象
自己写了一个例子 Xcode没报错,编译时报错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| //"Rectangle.h"
@interface Rectangle : NSObject
@property int width,height;
-(void) setWidth: (int) w andHeight:(int) h;
- (int) area;
@end
#import "Rectangle.h"
@interface Square_c : NSObject
{
Rectangle *rect;
}
- (instancetype) init;
- (instancetype) initWithSide:(int) s;
- (void) setSide:(int) s;
- (int) side;
-(int) area;
@end
@implementation Square_c
- (instancetype) init {
self = [super init];
if (self) {
rect = [[Rectangle alloc] init];
}
return [self initWithSide:0];
}
- (instancetype) initWithSide:(int) s
{
self = [super init];
if (self)
[rect setWidth:s andHeight:s];
return self;
}
- (void) setSide:(int) s
{
[rect setWidth:s andHeight:s];
}
- (int) side
{
return rect.width;
}
-(int) area
{
return [rect area];
}
@end
int main (int argc,char * argv[])
{
@autoreleasepool {
Square_c *square_c = [[Square_c alloc] initWithSide:20];
}
}
|
Xcode编译后报错
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Ld /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1 normal x86_64
cd /Project/prog1
export MACOSX_DEPLOYMENT_TARGET=10.10
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -L/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug -F/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug -filelist /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1.LinkFileList -mmacosx-version-min=10.10 -fobjc-arc -fobjc-link-runtime -Xlinker -dependency_info -Xlinker /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/prog1_dependency_info.dat -o /Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Products/Debug/prog1
duplicate symbol _OBJC_IVAR_$_Square2.rect in:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicate symbol _OBJC_CLASS_$_Square2 in:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
duplicate symbol _OBJC_METACLASS_$_Square2 in:
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/main.o
/Users/ma/Library/Developer/Xcode/DerivedData/prog1-eexmdvqxkooeorhbkjzcupkepwqy/Build/Intermediates/prog1.build/Debug/prog1.build/Objects-normal/x86_64/Square_composite.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) |
求熟手帮忙看下哪里出问题了