“重复符号错误”我没有导入.m文件

时间:2014-05-15 12:55:16

标签: objective-c

我刚开始学习"存取方法"目标C和我卡住了。我无法找到为什么我会得到这个"重复符号错误"阻止程序运行。应该是一件容易的事!但我会感激一些帮助。

xCode问题导航器抱怨:

Ld /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug/Shapes-Object normal x86_64
cd "/Users/leo/Google Drive/Documentos/Formación/Curso iOS/Pre iOS, pilares/Learn Objective C on the Mac/Ejercicios/Chapter 06/SPLIT 04.02 Shapes-Green-Circles MAS FACIL"
export MACOSX_DEPLOYMENT_TARGET=10.7
/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.9.sdk -                     L/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug -F/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug -filelist /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shapes-Object.LinkFileList -mmacosx-version-min=10.7 -fobjc-link-runtime -framework Foundation -Xlinker -dependency_info -Xlinker /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shapes-Object_dependency_info.dat -o /Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Products/Debug/Shapes-Object

duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Circulo.o
duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/SALCHICHA.o
duplicate symbol _colorPrint in:
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/main.o
/Users/leo/Library/Developer/Xcode/DerivedData/Shapes-Object-ctkpgnxalegjsdbbylyuaqxtisrd/Build/Intermediates/Shapes-Object.build/Debug/Shapes-Object.build/Objects-normal/x86_64/Shape.o
ld: 3 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是我的main()

#import <Foundation/Foundation.h>

#import "Circulo.h"
#import "SALCHICHA.h"



void FuncionDibujarFormas (id Formas[], int count)
{
for (int i = 0; i < count; i++) {
    id shape = Formas[i];
    [shape Dibujar];
}

} // FuncionDibujarFormas





// --------------------------------------------------
// The main function.  Make the shapes and draw them

int main (int argc, const char * argv[]) 
{
id Formas[3];


Formas[0] = [Circulo new];
[Formas[0] definirColor: kRojo];

Formas[2] = [SALCHICHA new];
[Formas[2] definirColor: kVerde];




FuncionDibujarFormas (Formas, 3);

return (0);

} // main

那是&#34;超类&#34; Shape.h的标头:

#import <Foundation/Foundation.h>

typedef enum {

kRojo,
kVerde,

} Color;


//--------------------------------------------------

NSString *colorPrint (Color switchColor) //"Color" se asigna "switchColor"
{
switch (switchColor) {
    case kRojo:
        return @"rojo";
        break;
    case kVerde:
        return @"verde";
        break;

}

return @"malign@";

 } // colorPrint

 @interface Shape : NSObject
 {
Color   RellenarColor;

 }

 - (void) definirColor: (Color) RellenarColor;

 - (void) Dibujar;


 @end // Shape

那是Shape.m

#import "Shape.h"

@implementation Shape

- (void) definirColor: (Color) ImplementationRellenarColor  
{

RellenarColor = ImplementationRellenarColor;

} // definirColor

- (void) Dibujar
{
} // draw

@end // Shape

那是&#34;子类&#34; Circulo.h

#import "Shape.h"

@interface Circulo : Shape
@end // Circulo  

这是儿童班Circulo.m:

#import "Circulo.h"

@implementation Circulo

- (void) definirColor: (Color) ImplementationRellenarColor
{
if ( ImplementationRellenarColor == kRojo ) { 

    ImplementationRellenarColor = kVerde;
}
[super definirColor: ImplementationRellenarColor];

} // definirColor



- (void) dibujar
{
NSLog (@"dibujando un circulo %@", colorPrint(RellenarColor)); 
} // dibujar

@end // Circulo

那&#34; chilclass&#34; SALCHICHA.h

#import "Shape.h"


@interface SALCHICHA : Shape

@end // SALCHICHA

Thats chilclass SALCHICHA.m

#import "SALCHICHA.h"

@implementation SALCHICHA

- (void) dibujar
{
NSLog (@"dibujando una SALCHICHA %@", colorPrint(RellenarColor)); 

} // dibujar


@end // SALCHICHA

1 个答案:

答案 0 :(得分:3)

在头文件(Shape.h)中有一个C函数实现: NSString *colorPrint (Color switchColor) //"Color" se asigna "switchColor" 每次导入标题时,都会复制符号。

所以在纯粹的Objc中使用你应该有类似的东西:

在标题中:

+(NSString*)colorPrint:(Color)switchColor;

在实施中:

+(NSString*)colorPrint:(Color)switchColor {
    switch (switchColor) {
        case kRojo:
            return @"rojo";
        case kVerde:
            return @"verde";
    }
    return @"malign@";
}

并称之为:

NSLog (@"dibujando un circulo %@", [Shape colorPrint:RellenarColor]);

编辑:修复参数名称