Basic objective c question

Discussion in 'Mac Programming' started by mikesown, Jan 24, 2007.

  1. macrumors member

    Joined:
    Aug 12, 2004
    #1
    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):(float):(float)c
    The function is called as follows:

    PHP:
    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
     
  2. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    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 :
     
  3. thread starter macrumors member

    Joined:
    Aug 12, 2004
    #3
    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
     
  4. thread starter macrumors member

    Joined:
    Aug 12, 2004
    #4
    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):(float):(float)c;
    @
    end
    Quadratic.m
    PHP:
    #import "Quadratic.h"

    @implementation Quadratic
    -(float)SolveQuadratic:(float):(float):(float){
        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 abcsol;
    = [A floatValue];
    = [B floatValue];
    = [C floatValue];
    float solu = [Quadratic SolveQuadratic:::c];
    [
    Solution setFloatValue:a];
    }

    @
    end
     
  5. macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #5
    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.
     

Share This Page