Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8821469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:51:47+00:00 2026-06-14T05:51:47+00:00

Copy-pasted code for the exercice from the programing in objective-c by Kochan from Program

  • 0

Copy-pasted code for the exercice from the “programing in objective-c” by Kochan from “Program 9.1”. But it doesn’t compile.

There are two classes: Fraction and Complex.
Complex’s objects seems to work fine, but Fraction’s object “fracResult” in “main.m” gives an error: “Assigning to “Fraction *” from incompatible type ‘void'”.

Here is Fraction.h:

#import <Foundation/Foundation.h>
// Define the Fraction class
@interface Fraction : NSObject
{
int numerator;
int denominator;
}
@property int numerator, denominator;
-(void) print;
-(void) setTo: (int) n over: (int) d;
-(double) convertToNum;
-(void) add: (Fraction *) f;
-(void) reduce;
@end 

Fraction.m file:

#import "Fraction.h"
@implementation Fraction
@synthesize numerator, denominator;
-(void) print
{
NSLog (@"%i/%i", numerator, denominator);
}
-(double) convertToNum
{
if (denominator != 0)
return (double) numerator / denominator;
else
return NAN;
}
-(void) setTo: (int) n over: (int) d
{
numerator = n;
denominator = d;
}
// add a Fraction to the receiver
-(void) add: (Fraction *) f
{
// To add two fractions:
// a/b + c/d = ((a*d) + (b*c)) / (b * d)
numerator = numerator * f.denominator + denominator * f.numerator;
denominator = denominator * f.denominator;
}
-(void) reduce
{
int u = numerator;
int v = denominator;
int temp;
while (v != 0) {
temp = u % v;
u = v;
v = temp;
}
numerator /= u;
denominator /= u;
}
@end

main.m file:

#import "Fraction.h"
#import "Complex.h"
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
Fraction *fracResult;
Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult;
[f1 setTo: 1 over: 10];
[f2 setTo: 2 over: 15];
[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];
// add and print 2 complex numbers
[c1 print]; NSLog (@" +"); [c2 print];
NSLog (@"---------");
compResult = [c1 add: c2];
[compResult print];
NSLog (@"\n");
[c1 release];
[c2 release];
[compResult release];
// add and print 2 fractions
[f1 print]; NSLog (@" +"); [f2 print];
NSLog (@"----");
fracResult= [f1 add: f2];  //this line gives an error 
[fracResult print];
[f1 release];
[f2 release];
[fracResult release];
[pool drain];
return 0;
}

By researching on this topic, i found out that often, this type of error caused by incorrect use of pointers. It was suggested, to simply remove the asterisk sign from declaration and implementation of method “add:”. But that led to more “semantic issues”

Also, sometimes code from the book doesn’t compile because of difference in characters that were used in the book and that are used by xcode. Often, dash is different. But that also didn’t solve the issue with “incompatible type”.

Any advice or suggestion is welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:51:49+00:00Added an answer on June 14, 2026 at 5:51 am

    Problem is that add is defined as

    -(void) add: (Fraction *) f;
    

    because it adds the Fraction* f passed to the current object to the object itself. This means that it doesn’t return a new fraction that is the sum of the two fracion but it modifies the fraction itself. That’s why the return type is (void), otherwise it would have been -(Fraction*) add:(Fraction*)f;.

    So

    Fraction *fractResult = [f1 add:f2]
    

    tries to store the value returned by [Fraction add:] but the return value is void, which is a non value and can’t be stored anywhere. (That’s why you get assigning from incompatible type void)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have copy pasted the code from Blog tutorial in cakephp 2.2 but its not
I pretty much copy and pasted the code from the API PDF, but am
I've copy/pasted the code from this tutorial about sockets on Android . It builds
I copy-pasted from MSDN this code: using System.Security.Cryptography; byte[] buffer = enc.GetBytes(text); SHA1CryptoServiceProvider cryptoTransformSHA1
Ok just to know,I copy-pasted the code from the android website,so I don't think
I copy-pasted this code from my own previous working document, so I know the
I copy pasted the code from http://jqueryui.com/demos/slider/range.html for local testing. And what I see
I copy and paste code from this URL for creating and reading/writing a proc
Frequently I copy and paste code from my existing code base. Eclipse frequently brings
Are there tools out there which could automatically find copy-and-paste code among a set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.