2010-01-26 93 views
7

我在Delphi 2010中完全无法获得类声明。我已阅读文档,在网上阅读,也许我是个白痴,但是,我无法得到任何东西来编译。任何帮助将大量赞赏!无法在Delphi 2010中使用转发类声明

我敲了这两个米老鼠类。当然我知道他们需要构造函数等来实际工作,它只是我遇到的问题的演示。

我有类MyParent其中包含我的其他类MyChild的TList。没关系。但是在MyChild里面,我希望能够设置对其父对象的引用,而不是TList,而是MyParent类。

unit ForwardClassDeclarationTest; 

interface 

uses generics.collections;   

type 
    MyChild = Class 
    private 
    ParentObect:MyParent; <--I need to be able to make this accessable 
    public 
End; 

type 
    MyParent = Class 
    public 
    tlChildren:TList<MyChild>; 
End; 

implementation 

end. 

我需要这两个类之前创建一个向前声明,但我完全无法得到任何东西去。预先感谢任何倾向于帮助我的人。

回答

13

@csharpdefector试试这个代码

uses 
    Generics.Collections; 

type 
    MyParent = Class; // This is a forward class definition 

    MyChild = Class 
    private 
    ParentObect:MyParent; 
    public 
    End; 

    MyParent = Class // The MyParent class is now defined 
    public 
    tlChildren:TList<MyChild>; 
    end; 

implementation 

end. 

更多信息,你可以看到这个linkdelphibasics

+1

是的,谢谢你!我没有把两个类放在同一个类型语句中。 – csharpdefector 2010-01-26 01:45:14

+1

@Jim:他们需要使用相同的_type_关键字,否则会遇到“未完全定义”的编译器错误。见梅森答案。 – 2010-01-26 06:53:09

+0

@保罗是的,你是对的。我会收回我的评论。 – 2010-01-26 23:05:21

13

之前宣布MyChild,放:MyParent = class;然后声明MyChild。然后正确地声明MyParent。并且不要重复使用类型关键字。它表示一个类型声明块,而不是一个单独的类型声明,而类前向声明​​只能在同一个块内工作。

+0

谢谢梅森,我明白了。我没有把你的两个课都放在你提到的同一个模块里。 – csharpdefector 2010-01-26 02:06:30