![]() |
Delphi-Version: 5
E2070 unbekannte Direktive 'operator'
Hallo,
meinen Quellcode hae ich aus Freepascal übernommen. Ich war der Ansicht, dass Operatoren mit der Direktive 'operator' seit Delphi 2006 (Turbo Delphi) längst auch in Delphi bekannt sind. Ist dem doch nicht so oder muss ich da irgendwas einstellen, damit ich solche Operatoren genau so wie mit Freepascal definieren kann? Oder unterscheidet sich hier nur die Syntax? |
AW: E2070 unbekannte Direktive 'operator'
Ich kenne die Syntax in FreePascal nicht, aber in Delphi sieht das so aus:
Delphi-Quellcode:
type
TExample = record private FValue: Integer; public class operator Add(const AValue1, AValue2: TExample): TExample; class operator Implicit(const AValue: Integer): TExample; class operator Implicit(const AValue: TExample): Integer; class operator Implicit(const AValue: TExample): String; end; { TExample } class operator TExample.Add(const AValue1, AValue2: TExample): TExample; begin Result.FValue := AValue1.FValue + AValue2.FValue; end; class operator TExample.Implicit(const AValue: TExample): Integer; begin Result := AValue.FValue; end; class operator TExample.Implicit(const AValue: TExample): String; begin Result := AValue.FValue.ToString; end; class operator TExample.Implicit(const AValue: Integer): TExample; begin Result.FValue := AValue; end; // Beispiel: var Test: TExample; begin Test := 40; ShowMessage(Test + 2); end; |
AW: E2070 unbekannte Direktive 'operator'
Ah, ok, die Syntax unterscheidet sich also hier von der in Freepascal. Danke!
Aber eine Verständnisfrage habe ich doch noch: Wie Werte Ich den TExample Typ aus. Erhalte ich dann einfach TExample.FValue zurück? So hier: [delphi] var myVal: TExample; resVal: TExample; myVal.Implicit(100).FValue := ... oder resVal := myVal.Add(20,30); Dann resVal.FValue verwenden? |
AW: E2070 unbekannte Direktive 'operator'
Die Dinger heißen Operatoren, weil sie auch tatsächlich welche sind. Add ist z.B. dafür da, dass man Werte einfach mit“+“ hinzuaddieren kann. Implicit und Explicit sind für Zuweisungen mittels „:=„ zuständig.
|
AW: E2070 unbekannte Direktive 'operator'
@DeddyH: Das weiß ich, meine vorherige Frage bozog sich auf die Verwendung der Daten. Ist die Syntax in meinem Beitrag oben richtig, wenn ich die Wperatoren Ergebnisse auswerten will?
|
AW: E2070 unbekannte Direktive 'operator'
Zitat:
Wenn du das möchtest, musst du normale Methoden verwenden. Operatoren existieren nicht als normale Klassenmethoden mit diesem Namen (und deine Aufrufe passen auch gar nicht dazu, selbst wenn es ginge). Sie sind nur als Operatoren verwend- oder aufrufbar. Und irgendwie erweckt die Frage den Eindruck, dass du die Verwendung noch nicht ganz verstanden hast. Den Wert bekommst du über den implicit Typecast Operator direkt, ohne Zugriff auf FValue. Das zeigt ja auch mein Beispielaufruf. |
AW: E2070 unbekannte Direktive 'operator'
Zitat:
![]() Zitat:
Zitat:
Zitat:
|
AW: E2070 unbekannte Direktive 'operator'
Geht es hier eigentlich wirklich um Delphi 5, wie im OP angegeben?
Wenn ja: Delphi 5 kannte noch keine enhanced records. |
AW: E2070 unbekannte Direktive 'operator'
Zitat:
Eigentlich kann das weg, weil das immer nur zu "ist das wirklich Delphi 5"-Nachfragen führt. |
AW: E2070 unbekannte Direktive 'operator'
Zitat:
In diesem Fall steht aber ja im Beitrag worum es geht: Zitat:
|
AW: E2070 unbekannte Direktive 'operator'
Zitat:
|
AW: E2070 unbekannte Direktive 'operator'
Zitat:
Delphi-Quellcode:
Ist schon auf class Operator geändert. In Freepascal werden nur im Interfaceteil die Operatoren definiert und im Implementationsteil mit Operatorkopf und begin end implementiert, den Record brauche in in FP nicht, wobei da auch Sonderzeichen für den Operator zulässig sind, womit ich den Operator mit dem ansonsten für die Operation verwendeten Zeichen benennen kann, was in Delphi nicht der Fall ist, da muss ich ein Alphanumerisches Wort definieren wie Mul für Multiplikation, Sub für Subtraktion statt "*" für Multiplikation oder "-" für Subtraktion.
unit vipmath;
interface uses math,tools; const M_PI = 3.14159265358979323846; type v3dVector = {packed} record x,y,z,w:single; class operator Sub(a,b:v3dVector):v3dVector; //Bereits an Delphi angepasst, den Record brauche in in FP nicht class operator Mul (a,b: v3dVector):single; class operator Mul (a,b:v3dVector):v3dVector; class operator add (a,b:v3dVector):v3dVector; class operator Mul (a:v3dVector; s:single):v3dVector; class operator Mul (m:v3dMatrix;v:v3dVector):v3dVector; class operator Mul (a,b:v3dMatrix):v3dMatrix; end; v3dMatrix = array [0..3,0..3] of single; function aVector(x,y,z:single):v3dVector; //operator - (a,b:v3dVector):v3dVector; function v3dDot(a,b:v3dVector):single; //operator * (a,b:v3dVector):single; So werden die Operatoren in Freepascal definiert function v3dCross(a,b:v3dVector):v3dVector; //operator * (a,b:v3dVector):v3dVector; hier nun auskommentiert und im Implementationsteil Delphi gerecht definiert function v3dGetLength(v:v3dVector):single; function v3dNormalize(v:v3dVector):v3dVector; //operator + (a,b:v3dVector):v3dVector; //operator * (a:v3dVector; s:single):v3dVector; function v3dIdentity:v3dMatrix; //operator * (m:v3dMatrix;v:v3dVector):v3dVector; //operator * (a,b:v3dMatrix):v3dMatrix; function v3dMatrixMulIgnoreW(m:v3dMatrix;v:v3dVector):v3dVector; function v3dMatrixRotateX(a:single):v3dMatrix; function v3dMatrixRotateY(a:single):v3dMatrix; function v3dMatrixRotateZ(a:single):v3dMatrix; function v3dMatrixTranslate(v:v3dVector):v3dMatrix; function v3dInvertMatrixOrtho(m:v3dMatrix):v3dMatrix; procedure v3dDumpMatrix(matrix:v3dMatrix); implementation function aVector(x,y,z:single):v3dVector; begin result.x:=x; result.y:=y; result.z:=z; result.w:=sqrt(x*x + y*y + z*z); end; class operator v3dVector.Sub (a,b:v3dVector):v3dVector; var x,y,z:single; begin x:=a.x - b.x; y:=a.y - b.y; z:=a.z - b.z; result:=aVector(x,y,z); end; function v3dGetLength(v:v3dVector):single; begin v3dGetLength:=sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w); end; function v3dNormalize(v:v3dVector):v3dVector; var l:single; begin l:=v3dGetLength(v); result.x:=v.x / l; result.y:=v.y / l; result.z:=v.z / l; result.w:=v.w / l; end; function v3dDot(a,b:v3dVector):single; begin v3dDot:=a.x*b.x + a.y*b.y + a.z*b.z; end; function v3dCross(a,b:v3dVector):v3dVector; var x,y,z:single; begin result.x:=a.y * b.z - a.z * b.y; result.y:=a.z * b.x - a.x * b.z; result.z:=a.x * b.y - a.y * b.x; result.w:=1; end; class v3dVector.operator Mul (a,b:v3dVector):single; begin result:=v3dDot(a,b); end; class v3dVector.operator Mul (a,b:v3dVector):v3dVector; begin result:=v3dCross(a,b); end; class v3dVector.operator Add (a,b:v3dVector):v3dVector; var x,y,z:single; begin x:=a.x + b.x; y:=a.y + b.Y; z:=a.z + b.z; result:=aVector(x,y,z); end; class v3dVector.operator Mul (a:v3dVector; s:single):v3dVector; var x,y,z:single; begin x:=a.x * s; y:=a.y * s; z:=a.z * s; result:=aVector(x,y,z); end; function v3dIdentity:v3dMatrix; var m:v3dMatrix; begin m[0,0]:=1; m[0,1]:=0; m[0,2]:=0; m[0,3]:=0; m[1,0]:=0; m[1,1]:=1; m[1,2]:=0; m[1,3]:=0; m[2,0]:=0; m[2,1]:=0; m[2,2]:=1; m[2,3]:=0; m[3,0]:=0; m[3,1]:=0; m[3,2]:=0; m[3,3]:=1; v3dIdentity:=m; end; class v3dVector.operator Mul (m:v3dMatrix; v:v3dVector):v3dVector; begin result.x:=(m[0,0]*v.x)+(m[1,0]*v.y)+(m[2,0]*v.z)+(m[3,0]*v.w); result.y:=(m[0,1]*v.x)+(m[1,1]*v.y)+(m[2,1]*v.z)+(m[3,1]*v.w); result.z:=(m[0,2]*v.x)+(m[1,2]*v.y)+(m[2,2]*v.z)+(m[3,2]*v.w); result.w:=(m[0,3]*v.x)+(m[1,3]*v.y)+(m[2,3]*v.z)+(m[3,3]*v.w); end; function v3dMatrixMulIgnoreW(m:v3dMatrix;v:v3dVector):v3dVector; begin result.x:=m[0,0] * v.x + m[0,1] * v.y + m[0,2] * v.z; result.y:=m[1,0] * v.x + m[1,1] * v.y + m[1,2] * v.z; result.z:=m[2,0] * v.x + m[2,1] * v.y + m[2,2] * v.z; result.w:=v.w; end; class v3dVector.operator Mul (a,b:v3dMatrix):v3dMatrix; begin result[0,0]:=(a[0,0]*b[0,0])+(a[0,1]*b[1,0])+(a[0,2]*b[2,0])+(a[0,3]*b[3,0]); result[0,1]:=(a[0,0]*b[0,1])+(a[0,1]*b[1,1])+(a[0,2]*b[2,1])+(a[0,3]*b[3,1]); result[0,2]:=(a[0,0]*b[0,2])+(a[0,1]*b[1,2])+(a[0,2]*b[2,2])+(a[0,3]*b[3,2]); result[0,3]:=(a[0,0]*b[0,3])+(a[0,1]*b[1,3])+(a[0,2]*b[2,3])+(a[0,3]*b[3,3]); result[1,0]:=(a[1,0]*b[0,0])+(a[1,1]*b[1,0])+(a[1,2]*b[2,0])+(a[1,3]*b[3,0]); result[1,1]:=(a[1,0]*b[0,1])+(a[1,1]*b[1,1])+(a[1,2]*b[2,1])+(a[1,3]*b[3,1]); result[1,2]:=(a[1,0]*b[0,2])+(a[1,1]*b[1,2])+(a[1,2]*b[2,2])+(a[1,3]*b[3,2]); result[1,3]:=(a[1,0]*b[0,3])+(a[1,1]*b[1,3])+(a[1,2]*b[2,3])+(a[1,3]*b[3,3]); result[2,0]:=(a[2,0]*b[0,0])+(a[2,1]*b[1,0])+(a[2,2]*b[2,0])+(a[2,3]*b[3,0]); result[2,1]:=(a[2,0]*b[0,1])+(a[2,1]*b[1,1])+(a[2,2]*b[2,1])+(a[2,3]*b[3,1]); result[2,2]:=(a[2,0]*b[0,2])+(a[2,1]*b[1,2])+(a[2,2]*b[2,2])+(a[2,3]*b[3,2]); result[2,3]:=(a[2,0]*b[0,3])+(a[2,1]*b[1,3])+(a[2,2]*b[2,3])+(a[2,3]*b[3,3]); result[3,0]:=(a[3,0]*b[0,0])+(a[3,1]*b[1,0])+(a[3,2]*b[2,0])+(a[3,3]*b[3,0]); result[3,1]:=(a[3,0]*b[0,1])+(a[3,1]*b[1,1])+(a[3,2]*b[2,1])+(a[3,3]*b[3,1]); result[3,2]:=(a[3,0]*b[0,2])+(a[3,1]*b[1,2])+(a[3,2]*b[2,2])+(a[3,3]*b[3,2]); result[3,3]:=(a[3,0]*b[0,3])+(a[3,1]*b[1,3])+(a[3,2]*b[2,3])+(a[3,3]*b[3,3]); end; function v3dMatrixRotateX(a:single):v3dMatrix; var c,s:single; m:v3dMatrix; begin c:= cos(a); s:= sin(a); // | 1 0 0 0 | // | 0 c -s 0 | // | 0 s c 0 | // | 0 0 0 1 | m:= v3dIdentity; m[1,1]:= c; //m[1,2]:= -s; //orig //m[2,1]:= s; //orig m[1,2]:= s; //bero m[2,1]:= -s; //bero m[2,2]:= c; result:=m; end; function v3dMatrixRotateY(a:single):v3dMatrix; var c,s:single; m:v3dMatrix; begin c:= cos(a); s:= sin(a); // | c 0 s 0 | // | 0 1 0 0 | // | -s 0 c 0 | // | 0 0 0 1 | m:= v3dIdentity; m[0,0]:= c; // m[0,2]:= s; // orig // m[2,0]:= -s; // orig m[0,2]:= -s; // bero m[2,0]:= s; // bero m[2,2]:= c; result:=m; end; function v3dMatrixRotateZ(a:single):v3dMatrix; var c,s:single; m:v3dMatrix; begin c:=cos(a); s:=sin(a); // | c -s 0 0 | // | s c 0 0 | // | 0 0 1 0 | // | 0 0 0 1 | m:=v3dIdentity; m[0,0]:= c; // m[0,1]:= -s; // orig // m[1,0]:= s; // orig m[0,1]:= s; // bero m[1,0]:= -s; // bero m[1,1]:= c; result:=m; end; function v3dMatrixTranslate(v:v3dVector):v3dMatrix; var m:v3dMatrix; begin // | 1 0 0 tx | // | 0 1 0 ty | // | 0 0 1 tz | // | 0 0 0 1 | { m:= v3dIdentity(); m[0,3]:=v.x; m[1,3]:=v.y; m[2,3]:=v.z; result:=m; } result[0,0]:=1.0; result[0,1]:=0.0; result[0,2]:=0.0; result[0,3]:=0.0; result[1,0]:=0.0; result[1,1]:=1.0; result[1,2]:=0.0; result[1,3]:=0.0; result[2,0]:=0.0; result[2,1]:=0.0; result[2,2]:=1.0; result[2,3]:=0.0; result[3,0]:=v.x; result[3,1]:=v.y; result[3,2]:=v.z; result[3,3]:=1.0; end; function v3dInvertMatrixOrtho(m:v3dMatrix):v3dMatrix; var j,k:integer; begin for j:=0 to 3 do for k:=0 to 3 do begin result[j][k]:=m[k][j]; end; end; { var det:single; im:v3dMatrix; A2323,A1323,A1223,A0323,A0223,A0123,A2313,A1313,A1213,A2312,A1312,A1212,A0313,A0213,A0312,A0212,A0113,A0112:single; begin A2323:= m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]; A1323:= m[2, 1] * m[3, 3] - m[2, 3] * m[3, 1]; A1223:= m[2, 1] * m[3, 2] - m[2, 2] * m[3, 1]; A0323:= m[2, 0] * m[3, 3] - m[2, 3] * m[3, 0]; A0223:= m[2, 0] * m[3, 2] - m[2, 2] * m[3, 0]; A0123:= m[2, 0] * m[3, 1] - m[2, 1] * m[3, 0]; A2313:= m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]; A1313:= m[1, 1] * m[3, 3] - m[1, 3] * m[3, 1]; A1213:= m[1, 1] * m[3, 2] - m[1, 2] * m[3, 1]; A2312:= m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]; A1312:= m[1, 1] * m[2, 3] - m[1, 3] * m[2, 1]; A1212:= m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1]; A0313:= m[1, 0] * m[3, 3] - m[1, 3] * m[3, 0]; A0213:= m[1, 0] * m[3, 2] - m[1, 2] * m[3, 0]; A0312:= m[1, 0] * m[2, 3] - m[1, 3] * m[2, 0]; A0212:= m[1, 0] * m[2, 2] - m[1, 2] * m[2, 0]; A0113:= m[1, 0] * m[3, 1] - m[1, 1] * m[3, 0]; A0112:= m[1, 0] * m[2, 1] - m[1, 1] * m[2, 0]; det:= m[0, 0] * ( m[1, 1] * A2323 - m[1, 2] * A1323 + m[1, 3] * A1223 ) - m[0, 1] * ( m[1, 0] * A2323 - m[1, 2] * A0323 + m[1, 3] * A0223 ) + m[0, 2] * ( m[1, 0] * A1323 - m[1, 1] * A0323 + m[1, 3] * A0123 ) - m[0, 3] * ( m[1, 0] * A1223 - m[1, 1] * A0223 + m[1, 2] * A0123 ); det:= 1 / det; im[0, 0]:= det * ( m[1, 1] * A2323 - m[1, 2] * A1323 + m[1, 3] * A1223 ); im[0, 1]:= det * - ( m[0, 1] * A2323 - m[0, 2] * A1323 + m[0, 3] * A1223 ); im[0, 2]:= det * ( m[0, 1] * A2313 - m[0, 2] * A1313 + m[0, 3] * A1213 ); im[0, 3]:= det * - ( m[0, 1] * A2312 - m[0, 2] * A1312 + m[0, 3] * A1212 ); im[1, 0]:= det * - ( m[1, 0] * A2323 - m[1, 2] * A0323 + m[1, 3] * A0223 ); im[1, 1]:= det * ( m[0, 0] * A2323 - m[0, 2] * A0323 + m[0, 3] * A0223 ); im[1, 2]:= det * - ( m[0, 0] * A2313 - m[0, 2] * A0313 + m[0, 3] * A0213 ); im[1, 3]:= det * ( m[0, 0] * A2312 - m[0, 2] * A0312 + m[0, 3] * A0212 ); im[2, 0]:= det * ( m[1, 0] * A1323 - m[1, 1] * A0323 + m[1, 3] * A0123 ); im[2, 1]:= det * - ( m[0, 0] * A1323 - m[0, 1] * A0323 + m[0, 3] * A0123 ); im[2, 2]:= det * ( m[0, 0] * A1313 - m[0, 1] * A0313 + m[0, 3] * A0113 ); im[2, 3]:= det * - ( m[0, 0] * A1312 - m[0, 1] * A0312 + m[0, 3] * A0112 ); im[3, 0]:= det * - ( m[1, 0] * A1223 - m[1, 1] * A0223 + m[1, 2] * A0123 ); im[3, 1]:= det * ( m[0, 0] * A1223 - m[0, 1] * A0223 + m[0, 2] * A0123 ); im[3, 2]:= det * - ( m[0, 0] * A1213 - m[0, 1] * A0213 + m[0, 2] * A0113 ); im[3, 3]:= det * ( m[0, 0] * A1212 - m[0, 1] * A0212 + m[0, 2] * A0112 ); result:=im; end; } procedure v3dDumpMatrix(matrix:v3dMatrix); begin log('matrix'); log(floatstr(matrix[0,0])); log(floatstr(matrix[0,1])); log(floatstr(matrix[0,2])); log(floatstr(matrix[0,3])); log(floatstr(matrix[1,0])); log(floatstr(matrix[1,1])); log(floatstr(matrix[1,2])); log(floatstr(matrix[1,3])); log(floatstr(matrix[2,0])); log(floatstr(matrix[2,1])); log(floatstr(matrix[2,2])); log(floatstr(matrix[2,3])); log(floatstr(matrix[3,0])); log(floatstr(matrix[3,1])); log(floatstr(matrix[3,2])); log(floatstr(matrix[3,3])); end; begin end. Compiler meckert aber hier in der ersten Operatorzeile mit "Ungültige Operatordeklaration". Was mache ich da noch falsch? |
AW: E2070 unbekannte Direktive 'operator'
Diese kurzen Bezeichnungen gibt es in Delphi schlicht nicht. Die heißen z.B. Subtract oder Multiply, siehe Doku:
![]() |
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz