Ich versuche grad zur gewöhnung an Pascal eine kleine numrische Algebra Bibliothek zu schreiben.
Ich fange mit einer Matrix-Klasse an. Leider bekomme ich beim Versuch Operatoren zu Überladen fiese exceptions (external-SIGSEV in zeile 44; unit 1) die für mich absolut unlesbar sind
 
 Was hab ich hier bloß falsch Verstanden?
so sieht das Programm aus:
Code: Alles auswählen
 
program project1;
 
{$mode objfpc}{$H+}
 
uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, unit1
  { you can add units after this };
var
  mat1,mat2,mat3:TdoubleMatrix;
 
begin
  writeln('test prog' + sLineBreak);
  mat1:= TdoubleMatrix.create(2,2);
  mat2:= TdoubleMatrix.create(2);
  mat3:= mat1+mat2;
  readln;
end. 
 Code: Alles auswählen
 
unit Unit1;
 
{$mode objfpc}{$H+}
 
interface
 
uses
  Classes, SysUtils;
 
// definition of a matrix consisting of double values
type
  TdoubleArray = array of array of double;
  TdoubleMatrix = class
    private
      // data
      rows,columns: Cardinal;
      dataArray: TdoubleArray;
    public
      // constructor signitures
      constructor create(m,n:Cardinal);overload;
      constructor create(size:Cardinal);overload;
      constructor create(inputArray: TdoubleArray);overload;
      // properties
      property toArray: TdoubleArray read dataArray;
      // Functions
    function toString:AnsiString;overload;
  end;
 
operator +(A,b:TdoubleMatrix):TdoubleMatrix;
 
 
implementation
 
  operator +(A,B:TdoubleMatrix):TdoubleMatrix;
  var
    i,j:integer;
    outMatrix:TDoubleMatrix;
  begin
    if (A.columns = B.columns) and (A.rows = b.rows) then
    begin
      outMatrix.create(A.columns,A.rows);
      for i:= 1 to A.rows do begin
        for j:= 1 to A.columns do begin
          outMatrix.dataArray[i,j]:=A.dataArray[i,j] + B.dataArray[i,j];
        end;
      end;
    end;
    result:=A;
  end;
 
  // construc by dimensions
  constructor TdoubleMatrix.Create(m,n:Cardinal);
  begin
    rows:= m;
    columns:= n;
    SetLength(dataArray,rows,columns);
  end;
 
  // constructor for unit matrix
  constructor TdoubleMatrix.Create(size:Cardinal);
  var
    i: cardinal;
  begin
    rows:= size;
    columns:= size;
    SetLength(dataArray,rows,columns);
    for i:= 0 to size-1 Do
    begin
      dataArray[i,i]:= 1.0;
    end;
  end;
 
  // constructor for arbitrarry matrix
  constructor TdoubleMatrix.Create(inputArray:TdoubleArray);
  begin
    columns:= Length(inputArray);
    rows:= Length(inputArray[0]);
    dataArray:= inputArray;
  end;
 
  function TdoubleMatrix.toString:AnsiString;
  ...
end.
 Zu meinem Hintergrund:
Ich versuche mich seit fast einem Jahrzent mal wieder an Pascal. Meine letzten Erfahrungen damit wahren ein bisschen Delphi Programmierung im Informatik untericht in der Schule. In der Zwischenzeit hatte ich mit unterschiedlichen Objektorientierten Sprachen u.A. Java,Python,C++ zu tun. Bin also durchaus mit Grundkonzepten objektorientierten Designs vertraut. Allerdings bin ich kein Informatiker und würde mich auch nicht als Software entwickler oder Programmierer bezeichnen. Ich mache das eher im Hobby-Bereich.


 
  Verein
Verein 
 Links
Links Suche
Suche