Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
December 16, 2017, 09:23:28 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Modified 7.5 - Completely Lost  (Read 1958 times)
braveand
Newbie
*
Posts: 10


« on: September 08, 2011, 10:00:53 AM »

Hi,
I tried to modify the 7.5 trying to understand if everything was clear in my mind.. obviously not.

I declared a class SportCar and my goal was to store the result of an operation called AddGas: inside an object GasUp.

Similar to the 7.5 but the method only return an object instead of return and accept an object as parameter.

Doesn't compile, 1 error and 1 warning.
Any ideas about it?

Code: (Objective-C)
#import <Foundation/Foundation.h>


// ----- @interface section GasUp -----

@interface GasUp : NSObject
{
int gasUpQuantity;
int gasUpWeight;
}

- (void) printGasUpWeight;

@end


// ----- @implementation section GasUp -----

@implementation GasUp

- (void) printGasUpWeight
{
NSLog(@"GasUp weight is %i kg", gasUpWeight);
}

@end


// ----- @interface section SportCar -----

@interface SportCar: NSObject
{
int gasLevel;
int dryWeight;
int carWeight;
}

- (void) printGas;
- (GasUp*) addGas: (int) g;


@property int gasLevel, dryWeight;

@end


// ----- @implementation section SportCar -----

@implementation SportCar

@synthesize gasLevel, dryWeight;

- (void) printGas
{
NSLog(@"GasLevel after GasUp is %i litri", gasLevel);
}

- (GasUp*) addGas: (int) g
{
GasUp *myGasUp = [[GasUp alloc] init];

gasLevel = gasLevel + g;

myGasUp.gasUpWeight = gasLevel * 7/10; // 1Lt = 0.7kg of Gas   //Error: request for member 'gasUpWeight' in something not a structure or union

[self printGas];
[myGasUp printGasUpWeight];

}   //Warning: control reaches end of non-void function

@end


// ----- program section -----

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

SportCar *gto599 = [[SportCar alloc] init];

// Set Dry Weight to 1495 kg
[gto599 setDryWeight: 1495];

// Set Current GasLevel to 10 lt
[gto599 setGasLevel: 10];

// GusUp of 30 lt
[gto599 addGas: 30];

[gto599 release];

    [pool drain];
    return 0;
}



Error: request for member 'gasUpWeight' in something not a structure or union

Warning: control reaches end of non-void function
« Last Edit: September 08, 2011, 03:35:52 PM by braveand » Logged
dharr19
Full Member
***
Posts: 175


Email
« Reply #1 on: September 08, 2011, 10:47:48 AM »

Hi

for the code below to work you would either have to create your own setter and getter methods for gasUpWeight or use the @property and @synthesize directives
so the compiler will automatically create the setter and getter methods for you
 
Afterwards you will be able to use the '.' dot operator.

Code: (Objective-C)
 myGasUp.gasUpWeight = gasLevel * 7/10; // 1Lt = 0.7kg of Gas  

I noticed that addGas is expected to return a GasUp object but there is no return statement in that method.

Code: (Objective-C)
- (GasUp*) addGas: (int) g
{
   GasUp *myGasUp = [[GasUp alloc] init];
   
   gasLevel = gasLevel + g;
   
   myGasUp.gasUpWeight = gasLevel * 7/10; // 1Lt = 0.7kg of Gas   // ERROR HERE SEE BELOW
   
   [self printGas];
   [myGasUp printGasUpWeight];

}   // WARNING HERE SEE BELOW


Hope this helps
David
Logged
braveand
Newbie
*
Posts: 10


« Reply #2 on: September 08, 2011, 12:00:38 PM »

I didn't understand that we need to create setter and getter methids for use the dot notation..  Shocked
I'll try to fix both problems..
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 276


WWW
« Reply #3 on: September 08, 2011, 12:32:19 PM »

Even though SportCar is inheriting from GasUp, because you have not created the setters and getters for the gasUpWeight variable it will be private to GasUp, and not accessible from outside the GasUp class.

I hope that helps,
Nick
http://myfirstiphoneapp.co.uk
Logged
braveand
Newbie
*
Posts: 10


« Reply #4 on: September 08, 2011, 03:29:22 PM »

I used @property and @synthesize for create accessor methods and the Error disappear.

Now the program compile and generate an output but I can fix the warning.
Any ideas?

Code: (Objective-C)

... ... ...

// ----- @implementation section SportCar -----

@implementation SportCar

@synthesize gasLevel, dryWeight;

- (void) printGas
{
   NSLog(@"GasLevel after GasUp is %i litri", gasLevel);
}

- (GasUp*) addGas: (int) g
{
   GasUp *myGasUp = [[GasUp alloc] init];
  
   gasLevel = gasLevel + g;
  
   myGasUp.gasUpWeight = gasLevel * 7/10; // 1Lt = 0.7kg of Gas
  
   [self printGas];
   [myGasUp printGasUpWeight];

}   //Warning: control reaches end of non-void function

@end

... ... ...

« Last Edit: September 08, 2011, 03:36:42 PM by braveand » Logged
braveand
Newbie
*
Posts: 10


« Reply #5 on: September 08, 2011, 03:58:45 PM »

added a return as suggested from dharr19 and now everything is fine.

thanks dharr19 e fujilla.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.