【问题标题】:Inaccessible value causing Access violation [duplicate]导致访问冲突的不可访问值[重复]
【发布时间】:2015-10-14 09:51:50
【问题描述】:

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

当调用我的Create 方法时,我收到访问冲突错误00453359 和地址写入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; 

当我看到那行时,它说它是一个inaccessible value,至于那里的所有变量。

这是我班的其余部分:

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 strange inaccessible value (acess violation) o.O 并说我必须将项目设置编辑为:

调试信息:开启

本地符号:开启

优化:关闭。

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

【问题讨论】:

  • 您在哪一行/变量上收到错误
  • `fCC := iCC' 在类中的构造方法中

标签: delphi delphi-7


【解决方案1】:

改变

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

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

这是管理新类实例的正确方法。

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

在每个对象(类的实例)中,您都有一个名为 Self 的隐藏参数,更多信息请参见 Delphi Basics。您的问题是您没有创建该类的新实例,因此您的 self 变量为 nil

【讨论】:

  • 没错,谢谢你:)
  • 欢迎您@AidanQuinn。请关闭问题。
猜你喜欢
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
相关资源
最近更新 更多