【问题标题】:Pascal Access Violation when calling a variable in a class调用类中的变量时发生 Pascal 访问冲突
【发布时间】:2017-02-06 09:41:33
【问题描述】:

我在 Pascal 中编写了一些非常简单的代码,导致我出现此错误:

BugFixing.exe 项目引发异常类 EAccessViolation,并带有消息“模块“BugFixing.exe”中地址 0040F1EE 的访问冲突。写入地址 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.

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

【问题讨论】:

    标签: pascal delphi


    【解决方案1】:

    一个类必须在使用前被实例化(TClassType.create)。唯一的例外是类/静态方法,但您不会那样声明它们(无论如何这都不是基本用法)

    【讨论】:

    • 我尝试添加一个名为“Create”的构造函数并使用它来设置 MaximiseQuestion 的值,但这似乎会产生与我使用过程相同的错误。我误解了你的建议吗?
    • 在您的 delphi 手册或帮助中查找语法。它是 instancevariable:=tclasstype.create 而不是 instancevariable.create。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-07
    • 2019-05-25
    • 2013-08-18
    • 2016-06-22
    • 1970-01-01
    • 2013-02-25
    • 2018-04-17
    相关资源
    最近更新 更多