Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mikesown

macrumors member
Original poster
Aug 12, 2004
37
0
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:

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

The function is called as follows:

PHP:
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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

macrumors member
Original poster
Aug 12, 2004
37
0
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

macrumors member
Original poster
Aug 12, 2004
37
0
Here are the complete files(save for main.m) from the project, in case they will help you solve my problem:

Quadratic.h
PHP:
/* Quadratic */

#import <Cocoa/Cocoa.h>

@interface Quadratic : NSObject
{
}
-(float)SolveQuadratic:(float)a :(float)b :(float)c;
@end
Quadratic.m
PHP:
#import "Quadratic.h"

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

QuadraticController.h:
PHP:
/* 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
PHP:
#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

macrumors 68040
Nov 9, 2004
3,042
31
Salt Lake City, UT
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:

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

It should be something like:

Code:
-(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 (44 KB).

EDIT: I added code so that it actually computes and displays both solutions to the quadratic equation instead of just returning 10.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.