5

私は2つのユニットを持っています。最初の1つは私のインターフェースです:

use personas

interface

type
  Tllave = array[0..31] of byte;
  Tdatos = array of byte;

  ImyInterface = interface(IInterface)

    function nombre : string;
    function edad : integer;
    procedure resetear;
    function Proceso(datos : tdatos; cantidad : integer) : integer ;    
    procedure Iniciar(llave : Tllave);
  end;

2 番目のユニット、私のオブジェクト宣言:

use militares

interface

uses personas;

type

  Tmilitares = Class(TInterfacedObject, ImyInterface )
    public
      function nombre : string;
      function edad : integer;
      procedure resetear;
      function Proceso(datos : Tdatos; cantidad : integer) : integer ;    
      procedure Iniciar(llave : Tllave);
    published
      constructor create;
  end;

implementation

function tmilitares.Proceso(datos : tdatos; cantidad : integer) : integer ; // getting error !!
begin
  // ....
end;


procedure tmilitares.Iniciar(llave : Tllave); // getting error!!
begin
  // ....
end;

「proceso」関数と「iniciar」プロシージャでのみエラー メッセージが表示されます。

'Iniciar' の宣言が以前の宣言
と異なります 'Proceso' の宣言が以前の宣言と異なります。

配列パラメーターがあることに気付きました。パラメータの型は最初のユニットで定義されています。これらの型を 2 番目のユニットで定義すると、同じエラーが発生しますが、オブジェクトの宣言に表示されます。どうすればコンパイルできますか?

4

3 に答える 3

10

十分なコードを示していませんが、インターフェイス セクションでの宣言とメソッドの実装の間で、問題のある型 (Tdatosおよび)を再定義していることは明らかです。この再宣言は、別のユニットの形式か、ユニットの実装セクションにあります。TllaveTmilitaresusemilitares

これらの他の宣言を見つけると、問題を解決できるようになります。


質問の最後にあるあなたのコメントは、次のことを伝えています。

これらの型を 2 番目のユニットで定義すると、同じエラーが発生しますが、クラスの宣言に表示されます。

タイプを再定義しようとしたという事実は、理解の問題を示しています。型は一度だけ宣言する必要があります。それらを 2 回定義すると、2 つの異なる互換性のない型ができます。さらに悪いことに、彼らは同じ名前を持っています!usesタイプを一度定義し、ステートメントを介して他のユニットにインポートします。

于 2011-05-24T21:15:43.220 に答える
0

以下の作品。一致しない事前の宣言はありません。インターフェイス(私の例ではunit2)セクションを実装するクラスで示したものとは少し異なるパラメーターを使用して、iMyInterface.SomeProcedure(私の例ではunit1)を宣言していると思われます。ImyInterfaceを実装するものはすべて、すべてを実装する必要があることに注意してください。

ユニット1:

unit Unit1;


interface

type   
  Tllave = array[0..31] of byte;  

  Tdatos = array of byte;

  ImyInterface = interface(IInterface)

    function nombre : string;
    function edad : integer;
    procedure resetear;
    function Proceso(datos : tdatos; cantidad : integer) : integer ;
    procedure Iniciar(llave : Tllave);   end;

implementation


  //stuff.

end.

ユニット2:

unit Unit2;

interface

{$M+}

uses Unit1;

type

  Tmilitares = Class(TInterfacedObject, ImyInterface )
    public
      function nombre : string;
      function edad : integer;
      procedure resetear;
      function Proceso(datos : Tdatos; cantidad : integer) : integer ;    
      procedure Iniciar(llave : Tllave);
    published
      constructor create;
  end;

implementation


function Tmilitares.nombre: string;
begin

end;

function tmilitares.Proceso(datos : tdatos; cantidad : integer) : integer ; // no more error 
begin
  // ....
end;


constructor Tmilitares.create;
begin

end;

function Tmilitares.edad: integer;
begin

end;

procedure Tmilitares.resetear;
begin

end;

procedure tmilitares.Iniciar(llave : Tllave); // no more error.
begin
  // ....
end;

end.
于 2011-05-24T21:47:35.430 に答える
0

これは非常によく似た問題であるため、ここに回答として追加していますが、次のようなユニットでこれに遭遇しました:

unit Unit1;

interface
  uses Generics.Collections;

type
  TFoo = class
  end;

  TFooList = class(TObjectList<TFoo>)
    protected
      procedure Notify(const Item: TFoo; Action: TCollectionNotification); override;
  end;

implementation

uses Classes;

procedure TFooList.Notify(const Item: TFoo; Action: TCollectionNotification);
var
 sl : TStringList;
begin
  //
end;

end.

[dcc32 エラー] Unit1.pas(20): E2037 'Notify' の宣言が以前の宣言と異なります
[dcc32 エラー] Unit1.pas(12): E2065 前方宣言または外部宣言が満たされていません: 'TFooList.Notify' [dcc32 致命的なエラー] Project1 .dpr(6): F2063 使用されたユニット 'Unit1.pas' をコンパイルできませんでした

あることを理解するのに、認めたくないほど時間がかかりました

System.Classes :: TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);

と :

System.Generics.Collections :: TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);

レッスンは、自分のタイプを系統的にチェックすることです。 Ctrl型識別子の+CLICKは、コンパイラが使用している型の定義に移動します。これを修正するには、uses句を再編成するか、完全修飾型名を使用してください。

私たち自身のコードで型名を複製するという初歩的なエラーを作成する場合は十分に悪いですが、Emba が独自の RTL でそれを行う場合は二重に悪いです。

于 2016-03-11T14:55:44.560 に答える