0

这是我的代码。这很简单,但我不明白错误

#import <Foundation/Foundation.h>

@interface Car: NSObject

@property(nonatomic,retain) NSString *brand;
@property int year;

@end //Car Interface

#import "Car.h"

@implementation Car

@synthesize brand, year;

@end //Car Implementation

#import "Car.h"

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

    //Creo un nuovo oggetto
    Car *myCar = [[Car alloc] init];

    //Setto i parametri
    [myCar setBrand: @"BMW Z4"];

    NSLog (@"Inserisci data modello: ");
    scanf (" %i", &y); //E' buona norma lasciare uno spazio
    [myCar setYear: y];

    //Stampo a video i dati
    NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);

    return (0);
}

这是我得到的错误:

car.m:5:1: error: ivar 'brand' used by '@synthesize' declaration must be an existing iva
car.m:5:1: error: ivar 'year' used by '@synthesize' declaration must be an existing ivar
car.m:7:1: warning: incomplete implementation of class 'Car' [enabled by default]
car.m:7:1: warning: method definition for '-setBrand:' not found [enabled by default]
car.m:7:1: warning: method definition for '-brand' not found [enabled by default]
car.m:7:1: warning: method definition for '-setYear:' not found [enabled by default]
car.m:7:1: warning: method definition for '-year' not found [enabled by default]
4

3 回答 3

1

当复制并粘贴到新的基于 XCode Cocoa 的命令行工具项目中时,这可以正常工作。唯一的区别是我将您的代码添加到@autoreleasepool

主程序

#import <Foundation/Foundation.h>
#import "Car.h"

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

    @autoreleasepool {
        int y;
        
        //Creo un nuovo oggetto
        Car *myCar = [[Car alloc] init];
        
        //Setto i parametri
        [myCar setBrand: @"BMW Z4"];
        
        NSLog (@"Inserisci data modello: ");
        scanf (" %i", &y); //E' buona norma lasciare uno spazio
        [myCar setYear: y];
        
        //Stampo a video i dati
        NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);
    }
    return 0;
}

上面@Martin R. 的回答表明您使用的是GNUStep而不是 XCode,因此您可能希望添加该标签,或专门在 GNUStep 论坛或聊天室寻求建议。

于 2013-08-01T15:26:59.760 回答
0
    #import <Foundation/Foundation.h>

    @interface Car: NSObject
    {
    @protected 
            NSString *brand;
            int year;

    }

    @property(nonatomic,retain) NSString *brand;
    @property int year;

    @end //Car Interface

#import "Car.h";


    @implementation Car

    @synthesize brand, year;

    @end //Car Implementation


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

        //Creo un nuovo oggetto
        Car *myCar = [[Car alloc] init];

        //Setto i parametri
        [myCar setBrand: @"BMW Z4"];

        NSLog (@"Inserisci data modello: ");
        scanf (" %i", &y); //E' buona norma lasciare uno spazio
        [myCar setYear: y];

        //Stampo a video i dati
        NSLog(@"Marca: %@ Anno: %i", [myCar brand], [myCar year]);

        return (0);
    }
于 2014-01-15T01:57:25.863 回答
-1

您需要在界面中添加属性。

@interface Car {
    @protected
        NSString *brand;
        int year;
}


@property(nonatomic,retain) NSString *brand;
@property int year;

@end

而不是这个

@interface Car: NSObject

@property(nonatomic,retain) NSString *brand;
@property int year;

@end //Car Interface

应该工作 - 但我还没有尝试过。

于 2013-08-01T15:11:28.940 回答