在类中调用变量时发生Pascal访问冲突 [英] Pascal Access Violation when calling a variable in a class

查看:105
本文介绍了在类中调用变量时发生Pascal访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pascal中编写了一些非常简单的代码,使我收到此错误:

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


Project BugFixing.exe引发了异常类EAccessViolation消息模块BugFixing.exe中地址0040F1EE的访问冲突。写入地址00000004'。

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:

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.

和uLinearProgrammingMainLogic.pas:

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. :)

推荐答案

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

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)

这篇关于在类中调用变量时发生Pascal访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆