''Unable to invoke Code Completion due to errors in source code''错误(50分)

M

morsh

Unregistered / Unconfirmed
GUEST, unregistred user!
定义了一个类,当在buttonclick事件中输入“ttiger.”的时候出现错误提示:''Unable to invoke Code Completion due to errors in source code''代码如下:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
tanimal=class
protected
fname:string;
fsort:string;
public
property name:string read fname write fname;
property sort:string read fsort write fsort;
constructor create;virtual;
function feed:string;virtual;
end;
ttiger=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
tcamel=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
tpeafowl=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ani:tanimal;
implementation

{$R *.dfm}
constructor tanimal.create;
begin
fname:='老虎';
fsort:='肉食类动物';
end;
function tanimal.feed:string;
begin
result:='肉类';
end;
constructor tcamel.create;
begin
fname:='骆驼';
fsort:='草食类动物';
end;
function tcamel.feed:string;
begin
result:='草';
end;
constructor tpeafowl.create;
begin
fname:='孔雀';
fsort:='禽类';
end;
function tcamel.feed:string;
begin
result:='粮食';
end;
constructor ttiger.create;
begin
fname:='老虎';
fsort:='肉食类动物';
end;
function ttiger.feed:string;
begin
result:='肉类';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
[red]ani:=ttiger.[/red]
end;

end.
刚学dephi,请各位高手指点一下。
 
ttiger是个类,是个类型,不是个对象。所以应该声明一个ttiger类型的变量,然后才可以“.”,比如:

var
atiger: ttiger;
begin
ani:=atiger;
//atiger.abc;
end;
 
类型定义里面,把你自定义的那些类都放到TForm类的外面。此外应该没什么问题。
man8888:建议你去复习一下“多态”。

以后碰到这种问题,我的解决方法一般是先编译一遍,然后就会出具体的错误提示,找问题就好办了。
 
楼主的方法没有问题,是编译器的问题,先compile下,在写代码就ok,不是楼上说的问题
 
请问hanpengshan_00:是在写代码之前就compile吗?我试了,可是输入“.”时候,还是出错。
 
有没有试过我说的?
 
请问Corn3:“放在Tform类外面”是指在定义Tform类后再单独另外定义自定义类吗?如
type
tform1:tform
end;
type
自定义类
end;
如果是这样,还是不行。
 
把类型申明里面,自定义的那几个类,create函数后面的override换成overload试试。父类的create后面的virtual也去掉。
因为构造函数是类方法。其他的都是对象方法。
 
我试了试,试试不行,这是全部代码,如果可以,帮我调试调试吧,谢谢啦
unit Unit1;

interface

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

type
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
tanimal=class
protected
fname:string;
fsort:string;
public
property name:string read fname write fname;
property sort:string read fsort write fsort;
constructor create;virtual;
function feed:string;virtual;
end;
ttiger=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
tcamel=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
tpeafowl=class(tanimal)
public
constructor create;override;
function feed:string;override;
end;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ani:tanimal;
implementation

{$R *.dfm}
constructor tanimal.create;
begin
fname:='老虎';
fsort:='肉食类动物';
end;
function tanimal.feed:string;
begin
result:='肉类';
end;
constructor tcamel.create;
begin
fname:='骆驼';
fsort:='草食类动物';
end;
function tcamel.feed:string;
begin
result:='草';
end;
constructor tpeafowl.create;
begin
fname:='孔雀';
fsort:='禽类';
end;
function tcamel.feed:string;
begin
result:='粮食';
end;
constructor ttiger.create;
begin
fname:='老虎';
fsort:='肉食类动物';
end;
function ttiger.feed:string;
begin
result:='肉类';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ani:=ttiger.create;
edit1.text:=ani.name;
edit2.text:=ani.sort;
edit3.text:=ani.feed;
freeandnil(nil);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ani:=tcamel.create;
edit1.text:=ani.name;
edit2.text:=ani.sort;
edit3.text:=ani.feed;
freeandnil(nil);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ani:=tpeafowl.create;
edit1.text:=ani.name;
edit2.text:=ani.sort;
edit3.text:=ani.feed;
freeandnil(nil);
end;

end.
 
怎么回事啊,你的TForm类里面,控件的申明怎么跑到事件的申明后面去了?
还有,构造函数后面的override换成overload有没有试过?
把你自定义的那些类都放到TForm的后面!!!
 
好了 谢谢啦
 
接受答案了.
 

Similar threads

I
回复
0
查看
617
import
I
I
回复
0
查看
550
import
I
I
回复
0
查看
527
import
I
I
回复
0
查看
509
import
I
顶部