Pascal Access Violation when calling a variable in a class

不羁的心 提交于 2019-12-13 00:18:22

问题


I have made some very simple code in Pascal that is getting me this error:

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

The program consists of 2 modules: 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.

And 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.

If anyone could explain to me why this is creating an access violation, that'd be appreciated. Thanks in advance. :)


回答1:


A class must always be instantiated (TClassType.create) before use. The only exception to that are class/static methods, but you don't declare them that way (and that is not basic usage anyway)



来源:https://stackoverflow.com/questions/42064576/pascal-access-violation-when-calling-a-variable-in-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!