无法访问的值导致访问冲突 [英] Inaccessible value causing Access violation

查看:65
本文介绍了无法访问的值导致访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个制作自行车的程序(TObject)

I have a program that makes a bike (TObject)

当调用我的 Create 方法时,出现访问冲突错误 00453359 和地址 00000004 的写入.

When calling my Create method, I get an access violation error 00453359 and a write of address 00000004.

constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
  sModel: string);
begin
  fCC := iCC; // <- Here is the error
  fPrice := iPrice;
  fStroke := iStroke;
  fYear := iYear;
  fName := sName;
  fModel := sModel; 

当我看到那条线时,它说这是一个不可访问的值,就像那里的所有变量一样.

When I watch that line, it says that it is an inaccessible value, as for all the variables there.

这是我课程的其余部分:

Here is the rest of my class:

type
  MyBike = class(TObject)
  private
    fCC, fStroke, fYear, fPrice: Integer; //I will at a later stage use fPrice as a currency
    fName, fModel: string;
  public
    constructor Create(iPrice, iStroke, iYear, iCC: Integer; sName, sModel:
      string);
    function GetValues: string;
  end;

implementation

{ MyBike }

constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
  sModel: string);
begin
  fCC := iCC;
  fPrice := iPrice;
  fStroke := iStroke;
  fYear := iYear;
  fName := sName;
  fModel := sModel;
end;

和我的主要单位:

private
    { Private declarations }
    NewBike : MyBike;
  public
    { Public declarations }       
  end;

var
  Form1: TForm1;
  redtSavedObject: TRichEdit;
  btnClearSavedObject: TButton;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  btnSaveToText.Enabled := False;
  btnSavetodata.Enabled := False;
end;

procedure TForm1.btnSaveasObjectClick(Sender: TObject);
var
  Price, Year, CC, Stroke : Integer;
  Name, Model : String;

begin
  Price := StrToInt(edtPrice.Text); //All of these values are fine
  Year := StrToInt(edtYear.Text);
  CC := StrToInt(edtCC.Text);
  Stroke := StrToInt(edtStroke.Text);
  Name := edtName.Text;
  Model := edtModel.Text;

  NewBike.Create(Price, Stroke, Year, CC, Name, Model);

我看了这篇文章: Delphi奇怪的无法访问的值(违反访问)oO并说我必须将项目设置编辑为:

I looked at this post: Delphi strange inaccessible value (acess violation) o.O and says I must edit the project settings to:

调试信息:开

本地符号:开

优化:关闭.

我已经进行了重建,但仍然没有任何变化.我已经尽力重启电脑无济于事

Ive done a rebuild, still no change. Ive gone as far as restarting my pc to no avail

推荐答案

更改

NewBike.Create(Price, Stroke, Year, CC, Name, Model);

收件人

NewBike := MyBike.Create(Price, Stroke, Year, CC, Name, Model);

这是处理Class的新实例的正确方法.

Thats the correct way to mane a new instance of a Class.

当您创建类的新实例时,您将调用该类的构造函数( MyBike ),并将其重新调整值分配给变量 NewBike:= MyBike.Create( ...);`

When you create an new instance of a class you call then constructor on the Class (MyBike) and assign it's retun value to a variable NewBike := MyBike.Create(...);`

在每个对象(一个类的实例)中,都有一个名为 Self 的隐藏参数,有关

Inside every object (instance of a class) you have a hidden parameter called Self more info on Delphi Basics. The problem in your case was that you didn't create a new instance of the class and therefor your self variable was nil.

这篇关于无法访问的值导致访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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