【问题标题】:Why the compiler gives me this error : Declaration of 'GetItem' differs from previous declaration?为什么编译器给我这个错误:“GetItem”的声明与之前的声明不同?
【发布时间】:2012-07-11 13:59:43
【问题描述】:

这是给我错误的代码:

TAdvSmoothDockItems = class(TCollection)
private
  FOwner: TAdvSmoothDock;
  FOnChange: TNotifyEvent;
protected
  function GetItem(Index: Integer): TAdvSmoothDockItem; virtual; //YLM_TMS_01
  procedure SetItem(Index: Integer; const Value: TAdvSmoothDockItem); virtual; //YLM_TMS_01
  function GetOwner: TPersistent; override;
public
  constructor Create(AOwner: TAdvSmoothDock); overload; virtual;    //YLM_TMS_01
  function Add: TAdvSmoothDockItem; virtual;    //YLM_TMS_01
  function Insert(Index: Integer): TAdvSmoothDockItem; virtual;    //YLM_TMS_01
  property Items[Index: Integer]: TAdvSmoothDockItem read GetItem write SetItem; default;
  procedure Delete(Index: Integer); virtual;    //YLM_TMS_01
published
  property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;


TKHAdvSmoothDockItems = class(TAdvSmoothDockItems)
  private
    FOwner: TKHAdvSmoothDock;
    FOnChange: TNotifyEvent;
  protected
    function GetItem(Index: Integer): TKHAdvSmoothDockItem; override;
    procedure SetItem(Index: Integer; const Value: TKHAdvSmoothDockItem); override;
    function GetOwner: TPersistent; override;
  public
    constructor Create(AOwner: TKHAdvSmoothDock);
    function Add: TKHAdvSmoothDockItem;
    function Insert(Index: Integer): TKHAdvSmoothDockItem;
    property Items[Index: Integer]: TKHAdvSmoothDockItem read GetItem write SetItem; default;
    procedure Delete(Index: Integer); override;
  published
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

这段代码给了我以下错误:

“GetItem”的 E2037 声明与之前的声明不同

为什么?我已将基类中的 GetItem 声明为“虚拟”,然后我尝试在下一个类(TKHAdvSmoothDockItems)中覆盖它......?如何解决?

【问题讨论】:

标签: delphi overriding


【解决方案1】:

因为

function GetItem(Index: Integer): TAdvSmoothDockItem; virtual;

不同于

function GetItem(Index: Integer): TKHAdvSmoothDockItem; override;

结果类型必须相同。

【讨论】:

  • 但如您所见,结果不能相同,因为属性(项目)类型是 TKHAdvSmoothDockItem。
  • @user1512094 不确定您要达到什么目的,但如果您覆盖函数或过程,则类型(包括结果类型)必须完全相同。
  • 对不起,我不知道多个问题!你有什么文件可以阅读 stackoverflow.com 的规则,这样我就可以确保我没有违反规则!
  • 一般信息请阅读FAQ。为了问同样的问题,这里的人很快就会找到重复的并关闭它们。最好编辑您的问题并更好地描述您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 2018-06-28
  • 1970-01-01
  • 2012-08-17
  • 1970-01-01
相关资源
最近更新 更多