Show: Object Pascal C++
Display Preferences

E2211 Declaration of '%s' differs from declaration in interface '%s' (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Error and Warning Messages (Object Pascal) Index

A method declared in a class which implements an interface is different from the definition which appears in the interface. Probable causes are that a parameter type or return value is declared differently, the method appearing in the class is a message method, the identifier in the class is a field or the identifier in the class is a property, which does not match with the definition in the interface.


program Produce;

  type
    IBaseIntf = interface
      procedure p0(var x : Shortint);
      procedure p1(var x : Integer);
      procedure p2(var x : Integer);
    end;

    TBaseClass = class (TInterfacedObject)
      procedure p1(var x : Integer); message 151;
    end;

    TExtClass = class (TBaseClass, IBaseIntf)
      p2 : Integer;
      procedure p0(var x : Integer);
      procedure p1(var x : Integer); override;
    end;

  procedure TBaseClass.p1(var x : Integer);
  begin
  end;

  procedure TExtClass.p0(var x : Integer);
  begin
  end;

  procedure TExtClass.p1(var x : Integer);
  begin
  end;

begin
end.

Generally, as in this example, errors of this type are plain enough to be easily visible. However, as can be seen with p1, things can be more subtle. Since p1 is overriding a procedure from the inherited class, p1 also inherits the virtuality of the procedure defined in the base class.


program Solve;

  type
    IBaseIntf = interface
      procedure p0(var x : Shortint);
      procedure p1(var x : Integer);
      procedure p2(var x : Integer);
    end;

    TBaseClass = class (TInterfacedObject)
      procedure p1(var x : Integer); message 151;
    end;

    TExtClass = class (TBaseClass, IBaseIntf)
      p2 : Integer;

      procedure IBaseIntf.p1 = p3;
      procedure IBaseIntf.p2 = p4;

      procedure p0(var x : Shortint);
      procedure p1(var x : Integer); override;
      procedure p3(var x : Integer);
      procedure p4(var x : Integer);
    end;

  procedure TBaseClass.p1(var x : Integer);
  begin
  end;

  procedure TExtClass.p0(var x : Shortint);
  begin
  end;

  procedure TExtClass.p1(var x : Integer);
  begin
  end;

  procedure TExtClass.p3(var x : Integer);
  begin
  end;

  procedure TExtClass.p4(var x : Integer);
  begin
  end;

begin
end.

One approach to solving this problem is to use a message resolution clause for each problematic identifier, as is done in the example shown here. Another viable approach, which requires more thoughtful design, would be to ensure that the class identifiers are compatible to the interface identifiers before compilation.

Personal tools
In other languages