2013-06-14 2 views
1

며칠 동안 누군가이 코드가 오류를 나타내는 이유를 묻습니다. 나는 알아 내지는 것을 시도했다 그러나 couldnt, 그래서 나는 사람들이 저희를 도울 것이라는 점을 희망한다.이 호환되지 않는 유형 오류가 무엇입니까?

첫 번째 단위 : classe가 정의 된 곳 unit1;

interface 

type 
    TSomeGeneric<T> = class 
    private 
    function getSelf: TSomeGeneric<T>; 
    public 
    property This : TSomeGeneric<T> read getSelf; 
    end; 

implementation 

{ TSomeGeneric<T> } 

function TSomeGeneric<T>.getSelf: TSomeGeneric<T>; 
begin 
    Result := Self; 
end; 

end. 

Unit2 유닛은 : (1)는이 유닛은 Unit2 에 정의 CLASSE에 referente한다;

interface 

uses 
    Unit1; 

type 
    TSomeGeneric<T> = class(Unit1.TSomeGeneric<T>); 

implementation 

end. 

frmMain 수 : 그것은 어떤 점은 Unit2 부 ufrmMain이다 이용한다

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, 

    Unit2; 

type 
    TfrmMain = class(TForm) 
    procedure FormCreate(Sender: TObject); 
    private 
    { Private declarations } 
    procedure SomeProcedure(ASG : TSomeGeneric<Integer>); 
    public 
    { Public declarations } 
    end; 

var 
    frmMain: TfrmMain; 

implementation 

{$R *.dfm} 

{ TfrmMain } 

procedure TfrmMain.FormCreate(Sender: TObject); 
var ASG : TSomeGeneric<Integer>; 
begin 
    ASG := TSomeGeneric<Integer>.Create(); 
    with ASG do 
    try 
     //make use os the class ... 
     SomeProcedure(This); //error -> incompatible types 
    finally 
     Free(); 
    end; 
end; 

procedure TfrmMain.SomeProcedure(ASG: TSomeGeneric<Integer>); 
begin 
    //... 
end; 

end. 

내가이 시점 TSomeGeneric = 클래스 (Unit1.TSomeGeneric)에서 TSomeGeneric 다른 CLASSE 아닌 참조로 정의되어 있는지 의심스러운,하지만 난

+0

네, 확실히 다른 클래스입니다. 동일한 이름의 Unit1 클래스의 자손입니다. 그게 문제라고 의심되면 왜 그걸 확인하지 못하니? 대신 Unit2를 삭제하고 Unit1을 대신 사용하여 프로그램이 컴파일되고 작동하는지 확인하십시오. 'SomeProcedure (This) '대신'SomeProcedure (ASG)'를 호출하면 작동합니까? –

+0

예 SomeProcedure (ASG)가 사용될 때 작동하지만 ASG.This 속성이 전달 될 때 작동하지 않습니다. 그게 내가 잃어버린 ASG 때문이야. 사실이게 또 다른 클래스 야? Unit.TSomeGeneric 및 Unit2.TSomeGeneric 이 프로 시저 매개 변수에 필요합니까? – kabstergo

답변

4

이 컴파일러는 꽤 명확하게 확인하지 못할 . 문제가있는 줄은 하나입니다

SomeProcedure(This); 

오류 :

때문에 그 메소드 호출에서의
 
[dcc32 Error] E2010 Incompatible types: 'Unit2.TSomeGeneric' and 
'Unit1.TSomeGeneric' 

:

  • This

    유형 Unit1.TSomeGeneric<System.Integer>
  • SomeProcedure이다가 기대 유형은 Unit2.TSomeGeneric<System.Integer>입니다.

명시 적으로 서로 다르기 때문에 이러한 유형이 아닙니다.

type 
    TSomeGeneric<T> = class(Unit1.TSomeGeneric<T>); 

은 새로운 유형을 정의하며 호환되지 않는 두 가지 유형이 있습니다.

Unit2을 제거하면됩니다. 합리적인 코드 컴파일을 중지하는 것 이외의 용도로는 사용되지 않습니다.

+0

감사합니다. 또 다른 질문입니다. unit2에는 unit1에서이 클래스를 링크하는 방법이 있습니다. 다시 정의하지 않아도됩니까? 일반 클래스가 아니라면 좋아. 예 : TSomeSimpleType = Unit1.TSomeSimpleType. – kabstergo

+0

아니, 제네릭 형식의 별칭을 선언 할 수 없다. –

+0

괜찮 았어. 그게 전부라고 생각해. tyvm – kabstergo

관련 문제