繁体   English   中英

E2008 Delphi中索引属性声明的不兼容类型

[英]E2008 Incompatible types on an indexed property declaration in Delphi

继上一个问题之后 ,当我尝试编译它时,我在这一行得到错误不兼容的类型:

    Property player[i : integer] : TStringList read p;

我不确定为什么? 这是全班:

unit Battle;

interface

uses
  SysUtils,Dialogs,Classes,inifiles, StdCtrls;
type

  TPlayers = class
  Private
    p : array[1..20] of TStringList;
    FPlaceUnit: Boolean;
    FTeamCount: Integer;
  Public
    Property player[i : integer] : TStringList read p;
    property PlaceUnit : Boolean read FPlaceUnit write FPlaceUnit;
    procedure AddPlayer (PlayerNo : integer; player : String);
    property TeamCount : Integer read FTeamCount write FTeamCount;

    constructor Create;   virtual;
  End;


{Host class}
  THostPlayers = Class(TPlayers)
  Private
    FIsHost: string;
  Public
    constructor Create; override;
    property IsHost : string read FIsHost write FIsHost;
 End;


{Guest Class}
  TGuestPlayers = Class(TPlayers)
  Private
    FIsGuest: string;
  Public
    constructor Create; override;
    property IsGuest : string read FIsGuest write FIsGuest;
  End;

implementation

uses
main;
{constructor}
constructor TPlayers.Create;
begin
  p := TStringList.Create;
end;
constructor THostPlayers.Create;
begin
  inherited;  // Calls TPlayers.Create
  IsHost := 'No';
  PlaceUnit := true;
  TeamCount :=0;
end;
constructor TGuestPlayers.Create;
begin
  inherited;  // Calls TPlayers.Create
  IsGuest := 'No';
  PlaceUnit := true;
  TeamCount := 0;
end;

{ADD Player}
procedure TPlayers.AddPlayer(PlayerNo : integer; player : String);
  var
      CharINI : TIniFile;
  begin
      CharINI := Tinifile.Create(thisdir+'\char\charstats.ini');
      CharINI.ReadSectionValues(player,player[PlayerNo]);
      CharINI.Free;
  end;
end.

首先,属性'返回'一个TStringList,而字段p是一个TStringlist数组,这就是你得到不兼容类型错误的原因。

你会期望:

// This is not working
Property player[i : integer] : TStringList read p[i];  // Not supported...

解决这个问题。 但是你不能直接访问数组元素,因此你需要一个getter函数:

TPlayers = class
private
  function GetPlayer(i: integer): TStringList;

public
  Property player[i : integer] : TStringList read GetPlayer 
end;



function TPlayers.GetPlayer(i: integer): TStringList;
begin
  Result := p[i];
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM