Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 24, 2007, 07:22 PM   #1
mikesown
macrumors member
 
Join Date: Aug 2004
Basic objective c question

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 Code:
-(float)SolveQuadratic :(float):(float):(float)
The function is called as follows:

PHP Code:
float solu = [Quadratic SolveQuadratic :::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
mikesown is offline   0 Reply With Quote
Old Jan 24, 2007, 07:27 PM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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 :
Catfish_Man is offline   0 Reply With Quote
Old Jan 24, 2007, 07:29 PM   #3
mikesown
Thread Starter
macrumors member
 
Join Date: Aug 2004
Quote:
Originally Posted by Catfish_Man View Post
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 is offline   0 Reply With Quote
Old Jan 24, 2007, 07:37 PM   #4
mikesown
Thread Starter
macrumors member
 
Join Date: Aug 2004
Here are the complete files(save for main.m) from the project, in case they will help you solve my problem:

Quadratic.h
PHP Code:
/* Quadratic */

#import <Cocoa/Cocoa.h>

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

@implementation Quadratic
-(float)SolveQuadratic:(float):(float):(float){
    return (float)
10.0;
}
@
end 
QuadraticController.h:
PHP Code:
/* 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 Code:
#import "QuadraticController.h"

@implementation QuadraticController

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

@
end 
mikesown is offline   0 Reply With Quote
Old Jan 24, 2007, 08:24 PM   #5
mduser63
macrumors 68040
 
mduser63's Avatar
 
Join Date: Nov 2004
Location: Salt Lake City, UT
Send a message via AIM to mduser63
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.

Last edited by mduser63; Jan 24, 2007 at 08:40 PM.
mduser63 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Snow Leopard File Vault Question and Basic System Password Question & Time Machine? GordonGekko999 Mac Basics and Help 0 Oct 25, 2013 06:06 AM
Basic Objective-C Cocoa questions? cpuin Mac Programming 8 Jun 3, 2013 07:10 PM
Basic Objective-C Questions xShane Mac Programming 12 Mar 18, 2013 11:08 AM
Objective-C / Java Question Mvkoe Mac Programming 3 Dec 8, 2012 08:27 AM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 02:44 PM

Forum Jump

All times are GMT -5. The time now is 07:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC