由于指针问题,无法在 Delphi 4 中编译旧项目
Old project cannot be compiled in Delphi 4 due to pointers issue
我对 Delphi 一点经验都没有,我有一个非常老的项目可以在第 2、3 个版本的 Delphi 中编译,但在 Delphi 4. 问题是指针在新版本中的工作方式不同。
这些代码导致错误“需要变量”:
pEnabled := @pClrWire_s^.enabled;
pEnabled := @Enabled;
pNEnabled := @pName_s^.Enabled;
其中 pEnabled 是:
const
pEnabled : ^boolean = nil;
和pClrWire_s和pName_s也是指针:
pClrWire_s : TpImage; {pointer to an image of colored wire}
pName_s : TpNamed; {pointer to the identifier}
TpImage和TpNamed的描述见项目其他文件:
type
TpImage = ^TImage;
TpNamed = ^TNamed;
TNamed = class(TLabel)
不认真重写整个代码能解决这个问题吗?是什么导致 Delphi 4 出现这样的问题?
这是使用最新 Delphi 测试的完整解决方案(D10.3 确实是 Delphi 版本 26,如果您与早期 Delphi 版本号进行比较,但那是另一个故事):
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
type
TpImage = ^TImage;
{$J+} // enable writable constant
const
pEnabled : ^boolean = nil;
var
pClrWire_s : TpImage;
type
TForm1 = class(TForm)
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
正如 Dalija Prasnikar 和 Gerry Coll 在他们各自的评论中所说,关键是启用可赋值类型常量。您可以使用源代码中的 {$J+} 或使用位于 Project options / Building / Delphi compiler / Compiling / Syntax options / Asignable typed constant.
的项目选项来完成。
让我知道它是否适合你。
以下最小程序在 Delphi4 中运行良好:
program ProjTestWriteableConstants;
{$APPTYPE CONSOLE}
{$J+}
type
TMyPt = ^Boolean;
const
pBool : TMyPt = nil;
var
Enabled : Boolean;
begin
pBool := @Enabled;
end.
来自 Delphi 4 个文档:
Type Switch
Syntax {$J+} or {$J-}{$WRITEABLECONST ON} or {$WRITEABLECONST OFF}
Default {$J+}{$WRITEABLECONST ON}
Scope Local
The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error.
In previous versions of Delphi and Borland Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.
注:
- 与以前版本的变化:在Delphi和Borland Pascal的以前版本中,类型化常量总是可写的,对应于{$J+}状态。使用可写类型常量的旧源代码必须在 {$J+} 状态下编译...
- 编译器指令{$J+}的范围是局部的,这意味着每个声明可写常量的单元都必须包含{$J+}开关。
结论:要使其工作,必须将编译器指令 {$J+} 放在声明可写常量的每个单元中。
我对 Delphi 一点经验都没有,我有一个非常老的项目可以在第 2、3 个版本的 Delphi 中编译,但在 Delphi 4. 问题是指针在新版本中的工作方式不同。
这些代码导致错误“需要变量”:
pEnabled := @pClrWire_s^.enabled;
pEnabled := @Enabled;
pNEnabled := @pName_s^.Enabled;
其中 pEnabled 是:
const
pEnabled : ^boolean = nil;
和pClrWire_s和pName_s也是指针:
pClrWire_s : TpImage; {pointer to an image of colored wire}
pName_s : TpNamed; {pointer to the identifier}
TpImage和TpNamed的描述见项目其他文件:
type
TpImage = ^TImage;
TpNamed = ^TNamed;
TNamed = class(TLabel)
不认真重写整个代码能解决这个问题吗?是什么导致 Delphi 4 出现这样的问题?
这是使用最新 Delphi 测试的完整解决方案(D10.3 确实是 Delphi 版本 26,如果您与早期 Delphi 版本号进行比较,但那是另一个故事):
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
type
TpImage = ^TImage;
{$J+} // enable writable constant
const
pEnabled : ^boolean = nil;
var
pClrWire_s : TpImage;
type
TForm1 = class(TForm)
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
end.
正如 Dalija Prasnikar 和 Gerry Coll 在他们各自的评论中所说,关键是启用可赋值类型常量。您可以使用源代码中的 {$J+} 或使用位于 Project options / Building / Delphi compiler / Compiling / Syntax options / Asignable typed constant.
的项目选项来完成。让我知道它是否适合你。
以下最小程序在 Delphi4 中运行良好:
program ProjTestWriteableConstants;
{$APPTYPE CONSOLE}
{$J+}
type
TMyPt = ^Boolean;
const
pBool : TMyPt = nil;
var
Enabled : Boolean;
begin
pBool := @Enabled;
end.
来自 Delphi 4 个文档:
Type Switch Syntax {$J+} or {$J-}{$WRITEABLECONST ON} or {$WRITEABLECONST OFF} Default {$J+}{$WRITEABLECONST ON} Scope Local
The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error.
In previous versions of Delphi and Borland Pascal, typed constants were always writeable, corresponding to the {$J+} state. Old source code that uses writeable typed constants must be compiled in the {$J+} state, but for new applications it is recommended that you use initialized variables and compile your code in the {$J-} state.
注:
- 与以前版本的变化:在Delphi和Borland Pascal的以前版本中,类型化常量总是可写的,对应于{$J+}状态。使用可写类型常量的旧源代码必须在 {$J+} 状态下编译...
- 编译器指令{$J+}的范围是局部的,这意味着每个声明可写常量的单元都必须包含{$J+}开关。
结论:要使其工作,必须将编译器指令 {$J+} 放在声明可写常量的每个单元中。