Lazarus Pascal -- Class 方法无法访问私有成员

Lazarus Pascal -- Class methods can't acces to private members

我遇到 Pascal-Lazarus (Linux) 的问题:

class 方法无法访问成员。这不是编译器错误,而是运行时错误。 (SIGSEV)

有关更多信息:我正在使用 Linux 最新版本 (16_4) 和 Lazarus Pascal (16.0)。我的系统类型是 x86_64

代码:

    unit compiler_code;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

    TLine = class
         public          //public methods
               procedure setLine(i: string);  //setter for the line.
               procedure compileLine();       //just runs the different methods of the class
         private         //private members
               var m_string       : string;
               var m_stringLength : integer;
         private         //private methods
               function  deleteBlanks (i: string) : string;
               procedure getStringLength();
    end;

var Form1: TForm1;
var Zeile: TLine;

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  Zeile.setLine ('Hallo');
  Zeile.compileLine ();
end;

/////////////////////////Implementation of the Methods of TLine

procedure TLine.setLine(i: string);  //Setter --> no getter needed.
begin
  showmessage (i);
  showmessage (m_string); //here is where the issue comes up
  //m_string:= i;
end;

procedure TLine.compileLine();   //runs all of the Methods.
begin
   getStringLength ();                  // gets the length of the String-input
   m_string := deleteBLanks(m_string); //deletes all of the blank space inside the String.
end;

function TLine.deleteBlanks (i: string) : string;  //blankSpace-Deleter
var isText : boolean = false; //switch, to check, if the momentary Character is text or not.
var counter: integer = 0; //counts the number of cycles of the loop
begin
     while ((counter < m_stringLength) and (not (m_stringLength = 0))) do //the 'Loop'
     begin
       if ((m_string[counter] = ' ') and (not(isText))) then
       begin
           delete (m_string, counter, 1); //deletes the blank position
           dec (counter);                 //because there is a position less in the string now.
           getStringLength();             //regenerates the length of the String;
       end;
     end;
end;

procedure TLine.getStringLength ();
begin
    m_stringLength:= length (m_string);          //gets the Length of the String input.
end;

{$R *.lfm}

end.

大概的解释是,您根本没有创建 class TLine 的实例。您没有分配给 Zeile,因此它保持默认值 nil

您需要实例化一个实例

Zeile := TLine.Create;

您必须在尝试引用 Zeile 之前执行此操作。完成实例后,将其销毁:

Zeile.Free;