0

Here it my code. It is very simple but I don't understand the error

#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);
}

Here the errors I got:

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]
Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • 3
    This is almost the same code as in your previous question http://stackoverflow.com/questions/17996426/any-mistakes-using-property-and-synthesize. Why do you accept the answer if it does not work for you? - And perhaps you should mention again that you are using **GNUstep**. – Martin R Aug 01 '13 at 15:09
  • Yes it is the same code but I solved the previosly problem infact I have anymore that error. Now the problem is different. – Mazzy Aug 01 '13 at 15:10
  • 1
    This is not about GNUstep. I'm using clang with GNUstep and I can compile your code just fine. – Fred Frith-MacDonald Aug 02 '13 at 09:24

3 Answers3

1

This works fine when copied and pasted in to a new XCode Cocoa-based Command Line Tool project. The only difference is I added your code in to the @autoreleasepool:

Main.m

#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;
}

The answer by @Martin R. above indicates you're using GNUStep instead of XCode, so you may wish to add that tag, or seek advice specifically on GNUStep forums or chat rooms.

Community
  • 1
  • 1
Josh Whittington
  • 705
  • 3
  • 10
  • 23
  • According what you say, programming in XCode is totally difference from programming in GNUstep so this is the really reason – Mazzy Aug 01 '13 at 15:30
  • 1
    @Mazzy - Are you referring to the @autoreleasepool? That's just an automated memory management feature of Objective-C (as far as my limited knowledge extends) - but yes, XCode is its own IDE for sure. – Josh Whittington Aug 02 '13 at 04:20
  • 1
    This isn't about GNUstep, it's about the compiler. GCC is the problem, or may be a too old version of clang. Don't use GCC with Objective-C. If you know any newcomer, tell them they should (must) use clang, not gcc. Uninstall GCC if it doesn't hurt anyone. – Fred Frith-MacDonald Aug 02 '13 at 09:31
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);
    }
Parvesh
  • 103
  • 1
  • 8
-1

you need to add the properties within the interface.

this

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


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

@end

instead of this

@interface Car: NSObject

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

@end //Car Interface

should work - but i haven't tried.

jowe
  • 32
  • 1