Hallo,
ich lerne gerade Delphi und versuche mich in OOP. Ich habe folgende Klasse für komplexe Zahlen geschrieben, die einige elementare Operationen zur Verfügung stellt:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
| unit Complex;
interface
uses math;
type TComplex = class private FRe : Extended; FIm : Extended; public constructor Create(Real, Imaginary: Extended); published property Re: Extended read FRe write FRe; property Im: Extended read FIm write FRe; function AbsVal: Extended; function ArgRad: Extended; procedure Add(z: TComplex); procedure Sub(z: TComplex); procedure Mult(z: TComplex); procedure Square; end;
implementation
constructor TComplex.Create(Real, Imaginary: Extended); begin FRe:=Real; FIm:=Imaginary; end;
function TComplex.AbsVal: Extended; begin Result:=Sqrt(FRe*FRe+FIm*FIm); end;
function TComplex.ArgRad: Extended; var temp: Extended; begin temp:=0; if (FRe=0) then begin if (FIm<>0) then temp:=(Pi/2)*(FIm/Abs(FIm)); end else begin temp:=arctan(FIm/FRe); if (FRe<0) then temp:=temp+(Pi/2); end; Result:=temp; end;
procedure TComplex.Add(z: TComplex); begin FRe:=FRe+z.Re; FIm:=FIm+z.Im; end;
procedure TComplex.Sub(z: TComplex); begin FRe:=FRe-z.Re; FIm:=FIm-z.Im; end;
procedure TComplex.Mult(z: TComplex); var temp: Extended; begin temp:=FRe; FRe:=FRe*z.FRe-FIm*z.FIm; FIm:=temp*z.FIm+FIm*z.Re; end;
procedure TComplex.Square; var temp: Extended; begin temp:=FRe; Re:=(FRe*FRe-FIm*FIm); Im:=(2*temp*FIm); end;
end. |
Nun das Problem: Ich schaffe es nicht, von dieser Klasse ein Objekt zu bilden.
Folgendes Programm liefert beim Klicken auf das Formular immer eine "EAccessViolation [..] write of adress 00000008 [..]".
Was mache ich so grundlegend falsch?
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Complex;
type TForm1 = class(TForm) procedure FormClick(Sender: TObject); private public end;
var Form1: TForm1; z: TComplex;
implementation
{$R *.dfm}
procedure TForm1.FormClick(Sender: TObject); begin z:=z.Create(0.0,0.0); end;
end. |
Das Programm ist quasi das "leere" Programm, das von Delphi erzeugt wird, wenn man File->New->Application auswählt. Ich habe lediglich die Unit Complex dem Projekt hinzugefügt und einen Eventhandler geschrieben, der ein Objekt erstellen soll, wenn man auf das Formular klickt.
Bin für jeden Hinweis dankbar!
Grüße
Duffman
Moderiert von
Tino: Delphi-Tags hinzugefügt.Moderiert von
Tino: Topic aus Sonstiges verschoben am Mo 07.03.2005 um 08:26