在 class 中调用变量时出现 Pascal 访问冲突

Pascal Access Violation when calling a variable in a class

我在 Pascal 中编写了一些非常简单的代码,但出现了这个错误:

Project BugFixing.exe raised exception class EAccessViolation with message 'Access violation at address 0040F1EE in module 'BugFixing.exe'. Write of address 00000004'.

该程序由 2 个模块组成: BugFixing.dpr:

program BugFixing;

{$APPTYPE CONSOLE}

uses
  SysUtils, uLinearProgrammingMainLogic in 'uLinearProgrammingMainLogic.pas', math;

var
MinOrMax : integer ;
Question : TQuestion ;

begin
  try
    Randomize ;
    MinOrMax := RandomRange(0,2) ;
    Question.SetMaximiseQuestion(MinOrMax);

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

和uLinearProgrammingMainLogic.pas:

    unit uLinearProgrammingMainLogic;

interface

uses sysUtils, math ;

type

TQuestion = class
  private
    MaximiseQuestion : boolean ;
  public
    procedure SetMaximiseQuestion (MinOrMax : integer) ;
end;

implementation

procedure TQuestion.SetMaximiseQuestion(MinOrMax : integer);
begin
  if MinOrMax = 0 then
    MaximiseQuestion := true
  else
    MaximiseQuestion := false ;
end;

end.

如果有人能向我解释为什么这会造成访问冲突,我们将不胜感激。提前致谢。 :)

A class 在使用前必须始终实例化 (TClassType.create)。唯一的例外是 class/static 方法,但您不会以这种方式声明它们(无论如何这都不是基本用法)