PDA

View Full Version : Basic objective c question




mikesown
Jan 24, 2007, 07:22 PM
I've recently begun to dabble in Objective C(comming from a background in java. I've been trying to create a function to do the quadratic formula, but I have had a problem with calling it. The function is defined as follows:

-(float)SolveQuadratic :(float)a :(float)b :(float)c

The function is called as follows:

float solu = [Quadratic SolveQuadratic :a :b :c];

where a,b,c, and solu are all float types. I get the error "incompatible types in initialization." I can't figure out for the life of me why this would happen, since I am using float values for everything. Can you guys help me? I appoligize if this is a question too elementary.

Thanks!
Mike



Catfish_Man
Jan 24, 2007, 07:27 PM
One issue I can see offhand is that you probably want +(float) not -(float), which would be equivalent to static in Java. Also I'm not sure that spaces are valid in method names, so you might need ...Quadratic: rather than ...Quadratic :

mikesown
Jan 24, 2007, 07:29 PM
One issue I can see offhand is that you probably want +(float) not -(float), which would be equivalent to static in Java. Also I'm not sure that spaces are valid in method names, so you might need ...Quadratic: rather than ...Quadratic :

Thanks for your reply!

Unfortunately, I intended for the method to be static, and changing it to be non-static leaves the same error(and creates some warnings!). I also tried deleting the space, but to no avail. Any other ideas?

Thanks,
Mike

mikesown
Jan 24, 2007, 07:37 PM
Here are the complete files(save for main.m) from the project, in case they will help you solve my problem:

Quadratic.h
/* Quadratic */

#import <Cocoa/Cocoa.h>

@interface Quadratic : NSObject
{
}
-(float)SolveQuadratic:(float)a :(float)b :(float)c;
@end

Quadratic.m

#import "Quadratic.h"

@implementation Quadratic
-(float)SolveQuadratic:(float)a :(float)b :(float)c {
return (float)10.0;
}
@end


QuadraticController.h:

/* QuadraticController */

#import <Cocoa/Cocoa.h>
#import "Quadratic.h"
@interface QuadraticController : NSObject
{
IBOutlet NSTextField *A;
IBOutlet NSTextField *B;
IBOutlet NSTextField *C;
IBOutlet id Quadratic;
IBOutlet NSTextField *Solution;
}
- (IBAction)Solve:(id)sender;
@end


QuadraticController.m

#import "QuadraticController.h"

@implementation QuadraticController

- (IBAction)Solve:(id)sender
{
float a, b, c, sol;
a = [A floatValue];
b = [B floatValue];
c = [C floatValue];
float solu = [Quadratic SolveQuadratic:a :b :c];
[Solution setFloatValue:a];
}

@end

mduser63
Jan 24, 2007, 08:24 PM
Ok, there are a few things sort of weird with your code. You've got the outlet to your Quadratic object named "Quadratic" the same as the class itself. You can't do that. It makes it so that that there's no way to tell if you're sending a message to (calling a method of) the class, or to the instance of the class. Changing the name of your outlet to myQuadratic will fix that, and the program will actually run.

However, while it will be technically correct at that point, you're violating a some styles rules for Objective-C. Don't use a capital letter for the first letter of a variable or method name (as in SolveQuadratic and Solution). Also, it's poor style in ObjC to not name each argument of a method.

You've got:

-(float)SolveQuadratic:(float)a :(float)b :(float)c

It should be something like:

-(float) solveQuadraticWithCoefficientA: (float)a andB: (float)b andC: (float)c

Another thing I'll point out is that while I realize that you're just learning, so it doesn't really matter in this case, normally there's really no reason to create an object for Quadratic. You can just use a function. Since it doesn't have any instance variables, and its only method simply does something with the arguments to the method itself, just use a function.

Anyway, I've uploaded a copy of an XCode project that works, and you can download it here (http://www.villainousturtle.com/misc/Quadratic.zip) (44 KB).

EDIT: I added code so that it actually computes and displays both solutions to the quadratic equation instead of just returning 10.