[SOLVED] Linked list inside a class. lazarus / 21-General ---------------------------------------------------------------------- seany · 2023-02-03 14:26 my adventure on making a markdown renderer with slightly more function continues  .. I have this linked list and class definition : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit TDocument; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, TRenderObjects, TBlockIdentifierEngine; type    SectionNodePTR = ^NodeStruct;    NodeStruct  = packed record      rendObj   : Variant;      ID        : Integer;      Parent    : SectionNodePTR;      Children  : Array of SectionNodePTR;      objType   : TBlockIdentifierEngine.classificationEnum;      end;    TNodeArray = Array of NodeStruct;    { markedDocument }   markedDocument = class  // Sections : Array of TRenderObjects.Section;  public      nodeTree                 : NodeStruct;      constructor Create();      procedure AppendNode(var ChildObj : NodeStruct);      procedure AppendObject_asNode(var insertionObject : variant; var ID : integer; var objType : TBlockIdentifierEngine.classificationEnum);    end;   The classificationEnum is defined as : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i classificationEnum = (localCommand, globalCommand, renderTarget, unknown, highLightCommand);   - this definition is in a different file: tblockidentifierengine.pas . This works. I want to add every block of renderable text (starting and ending with a newline) as well as every command block (starting with a . or period and ending with a ; or semicolon) as document nodes. The nodes will be marked either as renderTarget or as various commands. So The function is defined as: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure markedDocument.AppendObject_asNode(var insertionObject: variant;  var ID: integer; var objType: TBlockIdentifierEngine.classificationEnum);var   newNode                     : NodeStruct;   currNode                      : NodeStruct;begin    newNode.rendObj      := insertionObject;   newNode.ID           := ID;   newNode.objType      := objType;    if nodeTree = nil then   begin     nodeTree    := newNode;   end   else   begin     currNode    := nodeTree;     while length(currNode.Children) <> 0 do     begin       currNode  := currNode.Children[0];     end;     SetLength(currNode.Children, length(currNode.Children) + 1);     currNode.Children[length(currNode.Children) -1] := newNode;   end; end;          This is called as: classInstance.AppendObject_asNode("some text",0,a tesst enum value); But this does not compile. It tells me: In the line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if nodeTree = nil then Error: Operator is not overloaded: "NodeStruct" = "Pointer" I was hoping that, I can find out if the root NodeStruct in the class instance is populated or not via comparing it with nil. My plan is to add every block (text or command as described above) as a node in a linked list. In this list, each element is a Nodestruct. The first element of the linked list is denoted as nodeTree in the class. So if I see that the nodeTree is not set in an instance of the class, it will mean that that first element of the linked list is not set. So we can set the newly created NodeStruct with the incoming block directly as the nodeTree. Moreover, I also tried with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  if markedDocument.nodeTree = nil then  In that case, It tells me, that in the same line: Only class methods, class properties and class variables can be referred with class references Also, in this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i currNode  := currNode.Children[0]; I get an error: Error: Incompatible types: got "SectionNodePTR" expected "NodeStruct". I read that objects are already pointers, so there is no need to dereference them. But seems that is not the case? I have a similar error in this line. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i currNode.Children[length(currNode.Children) -1] := newNode; But I will be able to address that as soon as I have addressed the #2 error. Please help. Thank you. ---------------------------------------------------------------------- Blaazen · 2023-02-03 15:54 This is pretty complex question. Anyway, record cannot be nil. Pointer to record can. Declare Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  nodeTree: SectionNodePTR; and try Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if nodeTree = nil then   begin     new(nodeTree);     nodeTree^    := newNode;   end   else   begin Also here: correct is Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i currNode  := currNode.Children[0]^; ("Correct" means that both sides are of the same type; if it will do what you want is another story) ---------------------------------------------------------------------- seany · 2023-02-04 14:35 That needed a little bit of work. So this is how I did it. The class: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Classes, SysUtils, TRenderObjects, Dialogs, TBlockIdentifierEngine; type    genNodePtr = ^NodeStruct;    NodeStruct  = packed record      rendObj   : Variant;      ID        : Integer;      Parent    : genNodePtr;      Children  : Array of genNodePtr;      objType   : TBlockIdentifierEngine.classificationEnum;      end;    TNodeArray = Array of NodeStruct;        markedDocument = class  public      nodeTree                   : genNodePtr;      constructor Create();    procedure AppendNode(var ChildObj : NodeStruct);      procedure AppendObject_asNode(var insertionObject : variant; var ID : integer; var objType : TBlockIdentifierEngine.classificationEnum);    end;   Then the Insertion Program Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure markedDocument.AppendObject_asNode(var insertionObject: variant;  var ID: integer; var objType: TBlockIdentifierEngine.classificationEnum);var   newNode                     : ^NodeStruct;   currNode                      : ^NodeStruct;    // I made them pointers    argStr                        : String;begin    newNode               := New(genNodePtr);    newNode^.rendObj      := insertionObject;   newNode^.ID           := ID;   newNode^.objType      := objType;    // pointer is dereferenced with the caret after it's name, and then filled up    if nodeTree = nil then   begin     new(nodeTree);     nodeTree     := newNode;   end   else   begin      currNode    := nodeTree;      while length(currNode^.Children) <> 0 do                       // the last item in the list has no children, so children.length is 0     begin            currNode  := currNode^.Children[0];                          // Dereference and pick the first child, because at this stage the size of children is exactly ONE            end;          SetLength(currNode^.Children, length(currNode^.Children) + 1);      // increase arraysize from zero to one      currNode^.Children[length(currNode^.Children) -1] := newNode;       // Fill up first child. If i instead had currnode and newnode as records themselves, then                                                                        //  currNode.Children[length(currNode.Children) -1]^ := newNode compiles,                                                                        //  but throws a sigsev error in runtime   end; end; This works, I can look in the list, and it contains the stuff as i want them to... QUESTION Why the sigsev error (as shown in the code by a comment)? ---------------------------------------------------------------------- Blaazen · 2023-02-04 16:38 I'm surprised that this even compile: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  newNode := New(genNodePtr);It's not even documented: https://www.freepascal.org/docs-html/rtl/system/new.html I always used: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i new(newNode); BTW what is genNodePtr? ---------------------------------------------------------------------- seany · 2023-02-04 17:08 My bad. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i genNodePtr = ^NodeStruct; Also adjusted my previous post in this thread ---------------------------------------------------------------------- seany · 2023-02-04 17:11 Quote from: Blaazen on February 04, 2023, 05:38:15 pm It's not even documented: https://www.freepascal.org/docs- html/rtl/system/new.html About documentation: I followed it here: https://forum.lazarus.freepascal.org/index.php?topic=9521.0 The example code there looks like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TMailingListRecord = record    FirstName: string;    // [...]  end;  PMailingListRecord = ^TMailingListRecord;var  ptr: PMailingListRecord;begin  ptr := New(PMailingListRecord);                     // MY COMMENT: ptr is of type pointer to record, and is initialized as a new pointer to the same type of record  ptr^.FirstName := 'Per';  // [...]  Dispose(ptr);end;