简体   繁体   中英

“Incompatible Pointer to Integer Conversion”

I'm a complete "noob" to the world of Objective-C programming, and I've run into a stumbling block of sorts. I just got Stephen G. Kochan's "Programming in Objective-C, 4th Edition" the other day, and have been working through the exercises, which includes the creation of a Fraction class. Well, me being me, I decided to see if I could do a little "advanced" tinkering beyond the scope of the tutorial, by adding methods for the addition, subtraction, multiplication, and division. And wham, I run into things I don't know how to do yet ... (1) invoke class methods with (and without) arguments, and (2) assign their return values to variables of the same type as the return. The methods referenced are supposed to return integer (int) type numbers, but for some reason, I get the error: "Incompatible Pointer to Integer Conversion...etc" on both the assignment statements. Been banging my head against the wall for an hour on this. What am I doing wrong? Here's the code for the method in question:

-(void) add: frac1:(Fraction *) f1: (Fraction *) f2 
{ 
   int n1 = [Fraction: getNumerator()];
   int d1 = [Fraction: getDenominator()];
   // rest of method not yet written
}

--Andy H.

int n1 = [Fraction getNumerator];
int d1 = [Fraction getDenominator];

No need for the semicolon or parenthesis.

A method is made like such:

[ClassName methodName:paramater nextParam:param2];

and so on for the parameter names...

Also: your method declaration is wrong too:

-(void) addfrac1:(Fraction *) f1 withF2:(Fraction *) f2 

get rid of the add: and f1: .

I'm going to try and risk the wrath of downvotes and answer the question indirectly.

Firstly - the usual way to implement addition in a class is to provide a method to add something to itself. So rather than providing a method that adds two fractions - try providing a method that adds a fraction to itself and returns the result as a new Fraction object.

Secondly - using get in method names is conventionally used to denote a method that returns objects and values indirectly - as described in the Cocoa Coding guidelines

You're better off just using @properties for numerator and denominator rather than writing your own accessors.

So, IMO - your method could be:

- (Fraction *)addFraction:(NSFraction *)fraction {
    NSInteger numerator = self.numerator + fraction.numerator;
    NSInteger denominator = self.denominator + fraction.denominator;

    return [[NSFraction alloc] initWithNumerator:numerator andDenominator:denominator];
}

That assumes that you have the relevant properties and the initilaser, of course.

You have misunderstood how to declare an Objective-C method. You typed this:

-(void) add: frac1:(Fraction *) f1: (Fraction *) f2

Here's how the compiler interprets that. It sees a selector (method name) of add::: , with three arguments. The first argument is named frac1 . The second argument is named f1 . The third argument is named f2 . Since you didn't give a type for the frac1 argument, the compiler assumes the type id (which can refer to any object). So it's like you typed this:

- (void)add:(id)frac1 :(Fraction *)f1 :(Fraction *)f2

You could call that method like this:

id something;
Fraction *fraction1;
Fraction *fraction2;
[myCalculator add:something :fraction1 :fraction2];

But that's probably not what you want.

I think what you want to do is declare your method like this:

- (void)addFraction:(Fraction *)f1 toFraction:(Fraction *)f2;

Then you can call it like this:

Fraction *fraction1;
Fraction *fraction2;
[myCalculator addFraction:fraction1 toFraction:fraction2];

If Fraction is an Objective-C class, then inside your addFraction:toFraction: method, you will want to send your arguments messages to get their numerators and denominators, like this:

int n1 = [f1 numerator];
int d1 = [f1 denominator];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM