Objective C - Problem

  • problem (einfach mal lesen):

    ------------------------complex.h

    #import <objc/Object.h>

    @interface Complex : Object
    {
    double real;
    double imaginary;
    }

    -(void) setReal: (double) a;
    -(void) setImaginary: (double) b;
    -(void) setReal: (double) a andImaginary: (double) b;

    -(double) getReal;
    -(double) getImaginary;
    -(Complex *) add: (Complex *) f;

    -(void) print;
    @end

    ------------------------------complex.m

    #import "Complex.h"


    @implementation Complex

    -(void) setReal: (double) a
    {
    real = a;
    }

    -(void) setImaginary: (double) b
    {
    imaginary = b;
    }

    -(void) setReal: (double) a andImaginary: (double) b
    {
    real = a;
    imaginary = b;
    }

    -(double) getReal
    {
    return real;
    }

    -(double) getImaginary
    {
    return imaginary;
    }
    -(Complex *) add: (Complex *) InComp
    {
    Complex *OutComp = [[Complex alloc] init];

    [OutComp setReal: real + [InComp getReal] andImaginary: imaginary + [InComp getImaginary] ];

    return OutComp;
    }

    -(void) print
    {
    printf ("(%d + %di) ", real , imaginary ); // egal ob %di oder %d i (%d = single char)
    }


    @end

    --------------------------- main.m

    #import "Complex.h"

    int main(int argc, char *argv[])
    {
    Complex *Comp = [[Complex alloc] init];

    [Comp setReal: 0.1];
    [Comp setImaginary: 2.0];
    [Comp print];

    [Comp free];
    }

    -------------------------- Ergebnis in der console:
    (-1717986918 + 1069128089i)
    ähm ja ....
    bei
    [Comp setReal: 0.0];
    [Comp setImaginary: 0.0];
    kommt
    (0 + 0i)
    (auch bei statt double -> float)
    HILFE!!!!

    Edited once, last by Megge (August 15, 2008 at 8:49 PM).

  • :closed:
    problem gefunden

    Quote

    -(void) print
    {
    printf ("(%d + %di) ", real , imaginary ); // egal ob %di oder %d i (%d = single char)
    }

    müsste eigentlich
    -(void) print
    {
    printf ("(%g + %gi) ", real , imaginary ); // egal ob %di oder %d i (%d = single char)
    }
    oder
    -(void) print
    {
    printf ("(%f + %fi) ", real , imaginary ); // egal ob %di oder %d i (%d = single char)
    }
    sein , war einfach der falsche pointer :S .
    aber %d ist doch pointer auf eine Variable vom typ double oder nicht?

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!