Autor Beitrag
clSilver
Hält's aus hier
Beiträge: 3


D6 Pers
BeitragVerfasst: Sa 17.12.05 15:38 
Hallo!

Ich habe folgende Datenstruktur:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
Type
  zBaum=^TBaum;
  TBaum = Class
    inhalt:string;
    child1:zBaum;
    child2:zBaum;
   ...
  end;


ausblenden Delphi-Quelltext
1:
VAR Baum:zBaum;					


Folgende Operationen führen zu einer Access-Violation:

ausblenden Delphi-Quelltext
1:
2:
3:
new(Baum);
new(Baum^.child1); // Ist das nötig?
Baum.child1^.inhalt:='String';


Könnt ihr mir erklären warum? Wie muss ich vorgehen, um Baum^.child1^.inhalt etwas zuzuweisen?

Danke, Falk
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Sa 17.12.05 16:45 
Klassen sind bereits Pointer und werden im Allgemeinen nicht mit new() allokiert, sondern mit dem Konstruktor der Klasse (Create()).
Entweder, du ersetzt "class" durch "record", oder du bleibst bei deiner Klasse und wendest sie korrekt an.
Dabei solltest du die Pointer-Deklaration (zBaum) entfernen und die Variable vom Typ TBaum deklarieren. Dann geht's weiter mit
ausblenden Delphi-Quelltext
1:
2:
Baum := TBaum.Create;
Baum.Child1 := TBaum.Create
und dann sollte's auch mit Baum.Child1.Inhalt := 'Blubb'; klappen.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
AXMD
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 4006
Erhaltene Danke: 7

Windows 10 64 bit
C# (Visual Studio 2019 Express)
BeitragVerfasst: Sa 17.12.05 16:45 
Baum^.Child^.Inhalt := 'Bla', oder?

AXMD
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Di 20.12.05 11:16 
Erzeugen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
new(Baum);
Baum^ := tbaum.create;
new(Baum^.Child1);
new(Baum^.Child2);
Baum^.Child1^ := tbaum.create;
Baum^.Child1^ := tbaum.create;
Baum^.Inhalt := 'Text';


Freigeben:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
Baum^.Child1^.free;
Baum^.Child2^.free;
free(Baum^.Child1);
free(Baum^.Child2);
Baum^.free;
free(Baum);


Allerdings ist das eine ziemlich fiese Vermischung von Zeigern und Objekt-Referenzen. *grusel*

Beste Grüße
Tastaro
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Di 20.12.05 11:24 
user profile iconTastaro hat folgendes geschrieben:
Allerdings ist das eine ziemlich fiese Vermischung von Zeigern und Objekt-Referenzen. *grusel*
Allerdings... Und warum zeigst du ihm sowas dann auch noch, anstatt zu erklären, wie man es richtig macht?

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
Tastaro
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 414
Erhaltene Danke: 23



BeitragVerfasst: Di 20.12.05 13:18 
Weil das nicht die Frage beantwortet hätte. :)

Beste Grüße
Tastaro