Amazon.com Widgets void value error
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 09:18:36 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 7 - More on Classes
| | | |-+  void value error
Pages: [1]   Go Down
Print
Author Topic: void value error  (Read 709 times)
MuffinMan
Newbie
*
Posts: 10






« on: September 08, 2011, 05:53:11 AM »

This is the series sum of 1/2i code



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


@interface Fraction2 : NSObject {

int numerator;
int denominator;

}

@property int numerator, denominator;

-(void)print;
-(double)convertToNum;
-(void) setTo:(int)n over:(int)d;
-(void)reduce;
-(void)add: (Fraction2 *)f;


@end




Code: (Objective-C)
#import "Fraction2.h"


@implementation Fraction2

@synthesize numerator, denominator;

-(void)print {
NSLog(@"%i/%i",numerator,denominator);
}


-(void) setTo:(int)n over:(int)d {
numerator=n;
denominator=d;
}

-(double)convertToNum {

if (denominator!=0) {
return(double) numerator/denominator;
} else { return 1.0;}

}



-(void)reduce {

int u,v, temp;

u = numerator;
v= denominator;

while (v!=0) {
temp = u%v;

u = v;

v = temp;
}

numerator /= u;

denominator /=u;
}





-(void)add: (Fraction2 *)f {

// a/b + c/d =  (a*d + b*c)/ b*d

numerator = numerator* f.denominator + denominator * f.numerator;

denominator = denominator * f.denominator;

[self reduce];
[self print];

}
@end




Code: (Objective-C)
#import "Fraction2.h"

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



Fraction2 *sum =[[Fraction2 alloc]init], *sum2;

int n,i,pow2;

[sum setTo:0 over:1];


scanf("%i",&n);

pow2= 2;

for(i=1;i<=n;++i){

[aFraction setTo:1 over:pow2];

sum2 = [sum add: aFraction];
[sum release];

sum = sum2;

pow2 *= 2;

}


NSLog(@"The sum is %f:",sum);





sum.release;
sum2.release;





aFraction.release;



[pool drain];
return 0;
}


I am getting the "void value not ignored as it ought to be" error only at the line "sum2 = [sum add: aFraction];" in the program section, what I am doing wronag?
« Last Edit: September 08, 2011, 12:18:51 PM by MuffinMan » Logged
MuffinMan
Newbie
*
Posts: 10






« Reply #1 on: September 08, 2011, 05:57:23 AM »

and oh, don't mind the  [self print]; in the definition, I used this for a normal fraction before, i know i should remove it here....
Logged
seerex
Full Member
***
Posts: 177


Email




« Reply #2 on: September 08, 2011, 08:05:24 AM »

not sure if this fixes it, but you are trying to print the sum using the float print:
Code: (Objective-C)
NSLog (@"%f", sum);

You cant write it, since sum is a variable that holds a reference to an object.
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
dharr19
Full Member
***
Posts: 175


Email




« Reply #3 on: September 08, 2011, 08:30:34 AM »

Hi,

I noticed that the return type of the add method was void

Code: (Objective-C)
-(void)add: (Fraction2 *)f {  

but here you are expecting add to return a value to sum2

Code: (Objective-C)
sum2 = [sum add: aFraction];

Hope this helps
David
Logged
skochan
Administrator
Hero Member
*****
Posts: 3106







« Reply #4 on: September 08, 2011, 08:55:34 AM »

FYI, while it's syntactically correct to write a statement such as sum.release, it's not considered good programming style.   The dot operator was really meant to be used with property's; typically to set/get the value of an instance variable.   Other methods that do work (like calculate the sum of two fractions, release an object, etc.) are labeled as "Tasks" in Apple's documentations.  Tasks are typically not executed using the dot operator; the traditional bracketed message expression is the preferred syntax.

Cheers,

Steve
Logged
MuffinMan
Newbie
*
Posts: 10






« Reply #5 on: September 08, 2011, 12:46:31 PM »

FYI, while it's syntactically correct to write a statement such as sum.release, it's not considered good programming style.   The dot operator was really meant to be used with property's; typically to set/get the value of an instance variable.   Other methods that do work (like calculate the sum of two fractions, release an object, etc.) are labeled as "Tasks" in Apple's documentations.  Tasks are typically not executed using the dot operator; the traditional bracketed message expression is the preferred syntax.

Cheers,

Steve

Noted.

Thank you.
Logged
MuffinMan
Newbie
*
Posts: 10






« Reply #6 on: September 08, 2011, 12:52:12 PM »

I solved the problem and the outputs are correct,

My main mistake that I've put  #import Fraction2.h instead of #import Fraction3.h  ooops.


my other big mistake was the -(void) add: ,   I didn't see it because I thought I copied from the 7.5 code I made earlier, but it seems that I made my Fraction3 (7.6) over  a more earlier Fraction program where the method of add: was void.


I also corrected the other couple things mentioned by users here.   Thank you everyone.


Btw, that made me think of another question:

How can I properly duplicate a xCode  project?   I couldn't find a proper way to do it other than copying/pasting the codes one by one .....
Logged
fujilla
Global Moderator
Sr. Member
*****
Posts: 270


WWW




« Reply #7 on: September 08, 2011, 01:03:05 PM »

You could duplicate the project in the finder, however you will not be able to change the name of the copied project.

The best way that I know of is to create a new project, then drag and drop (from Xcode) the classes you want from the original project into the new project, and select the "Copy" radio button in the window that appears.

Nick
http://myfirstiphoneapp.co.uk
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

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 (c) 2009 classroomM.com. All rights reserved.