Undeclared identifier
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, untClassStudent, untClassTeacher, StdCtrls;
type
TForm1 = class(TForm)
lbTeacher: TListBox;
lbStudent: TListBox;
btnTeacher: TButton;
btnStudent: TButton;
procedure btnTeacherClick(Sender: TObject);
procedure btnStudentClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ATeacher:TTeacher;
AStudent:TStudent;
implementation
{$R *.dfm}
procedure TForm1.btnTeacherClick(Sender: TObject);
begin
ATeacher:=TTeacher.Create('马萍',Female,28,'讲师');
lbTeacher.Items.Add('教师姓名:'+ATeacher.Name); //显示ATeacher变量的Name域
lbTeacher.Items.Add('教师性别:'+ATeacher.Sex);
lbTeacher.Items.Add('教师年龄:'+IntToStr(ATeacher.Age));
lbTeacher.Items.Add('教师职称:'+ATeacher.TeachPost);
lbTeacher.Items.Add(ATeacher.Talk);
lbTeacher.Items.Add('类变量所分配的存储字节数:'+IntToStr(SizeOf(ATeacher)));
lbTeacher.Items.Add('类实例所分配的存储字节数:'+IntToStr(TTeacher.InstanceSize));
ATeacher.Free; //释放对象实例
Ateacher:=nil;
end;
procedure TForm1.btnStudentClick(Sender: TObject);
begin
AStudent:=TStudent.Create; //创建对象实例
AStudent.Name:='李大江';
AStudent.Sex:=True;
AStudent.Birthday:=EncodeDate(1986,2,18);
lbStudent.Items.Add('学生姓名:'+AStudent.Name); //显示ATeacher变量的Name域
if AStudent.Sex then
lbStudent.Items.Add('男')
else
lbStudent.Items.Add('女');
lbStudent.Items.Add('学生生日:'+DateToStr(AStudent.Birthday));
lbStudent.Items.Add(AStudent.Talk);
lbStudent.Items.Add('类变量所分配的存储字节数:'+IntToStr(SizeOf(AStudent)));
lbStudent.Items.Add('类实例所分配的存储字节数:'+IntToStr(TStudent.InstanceSize));
AStudent.Free; //释放对象实例
end;
end.
unit untClassTeacher;
interface
uses
controls, Dialogs, DateUtils;
type
TTeacher=class
private
FName:string; //姓名
FSex:Boolean; //性别
FBirthday:TDate; //出生日期
FTeachPost:string;
function GetAge: Integer;
function GetSex: string;
procedure SetAge(const Value: Integer);
procedure SetSex(const Value: string); //职称
public
constructor Create(NValue:string;SValue:Boolean;BValue:TDate;TPValue:string);overload;//构造创建方法(重载)
constructor Create(N,S:string;A:Integer;T:string);overload;
destructor Destroy;override; //析构方法,销毁对象(覆盖)
function Talk:string;
property Name:string read FName write FName;
property Sex:string read GetSex write SetSex;
property Age:Integer read GetAge write SetAge;
property TeachPost:string read FTeachPost write FTeachPost;
end;
implementation
{ TTeacher }
constructor TTeacher.Create(NValue:string;SValue:Boolean;BValue:TDate;TPValue:string); //TTeacher.一定要全称!!切记!
begin
FName:=NValue;
FSex:=SValue;
FBirthday:=BValue;
FTeachPost:=TPValue;
end;
function TTeacher.Talk:string;
begin
Result:='I am a teacher';
end;
destructor TTeacher.Destroy ;
begin
ShowMessage('Instance object has been destroyed.');
end;
constructor TTeacher.Create(N, S: string; A: Integer; T: string);
begin
Name:=N;
Sex:=S;
Age:=A;
TeachPost:=T;
end;
function TTeacher.GetAge: Integer;
begin
Result:=YearOf(Now)-YearOf(FBirthDay);
end;
function TTeacher.GetSex: string;
begin
if FSex then
Result:='男'
else
Result:='女';
end;
procedure TTeacher.SetAge(const Value: Integer);
begin
if(Value<=200) and (Value>0) then
FBirthDay:=EncodeDate(YearOf(Now)-Value,1,1)
else
raise Exception.Create('年龄输入有误');
end;
procedure TTeacher.SetSex(const Value: string);
begin
if(Value='男') or (UpperCase(Value)='MALE') or (UpperCase(Value)='TRUE') then
FSex:=True
else if(Value='女') or (UpperCase(Value)='FEMALE') or (UpperCase(Value)='FALSE') then
FSex:=False
else
raise Exception.Create('性别输入有误');
end;
end.
unit untClassStudent;
interface
uses
controls;
type
TStudent=class
Name:string; //姓名
Sex:Boolean; //性别
Birthday:TDate; //出生日期
function Talk:string;
end;
implementation
{ TStudent }
function TStudent.Talk:string;
begin
Result:='I am learning programing.';
end;
end.
运行出现的问题是:
[Error] untClassTeacher.pas(62): Undeclared identifier: 'YearOf'
[Error] untClassTeacher.pas(76): Undeclared identifier: 'EncodeDate'
[Error] untClassTeacher.pas(76): Undeclared identifier: 'YearOf'
[Error] untClassTeacher.pas(78): Undeclared identifier: 'Exception'
[Error] untClassTeacher.pas(78): There is no overloaded version of 'Create' that can be called with these arguments
[Error] untClassTeacher.pas(83): Undeclared identifier: 'UpperCase'
[Error] untClassTeacher.pas(83): Operator not applicable to this operand type
[Warning] untClassTeacher.pas(83): Combining signed and unsigned types - widened both operands
[Error] untClassTeacher.pas(85): Operator not applicable to this operand type
[Warning] untClassTeacher.pas(85): Combining signed and unsigned types - widened both operands
[Error] untClassTeacher.pas(88): Undeclared identifier: 'Exception'
[Error] untClassTeacher.pas(88): There is no overloaded version of 'Create' that can be called with these arguments
[Fatal Error] MainForm.pas(7): Could not compile used unit 'untClassTeacher.pas'
[解决办法]
是未加入函数所在的单元造成的。
untClassTeacher单元:
uses
controls, Dialogs, DateUtils, SysUtils;
[解决办法]
单元没有uses,比如日期的DateUtils,这些函数去帮助中都能查得到