各位大师帮忙看看....
我照书上敲了一段代码,是关于用TList类控制对象元素的,编译通过,但运行时显示出错,请各位帮忙看看
void __fastcall TForm1::FormPaint(TObject *Sender)
{//重画窗体。这段程序通过一个TList对象BugList对象遍历了所有元素,BugList声明在Form1.cpp中
TBug *tmpBug;//自己的定义的一个类
for(int i=0;i<BugList->Count;i++)//程序显示这里出错
{
tmpBug=(TBug *)BugList->Items[i];
tmpBug->Show();
}
问题点数:50、回复次数:9Top
1 楼invalid(空心菜(Python是个好东东,大家多用用!))回复于 2002-04-02 22:48:58 得分 10
没看出什么错误,试试下面的转换方式。
TBug *tmpBug;
for(int i=0;i<BugList->Count;i++)//程序显示这里出错
{
tembug=reinterpret_cast<TBug*>(BugList->Items[i])
tmpBug->Show();
}
Top
2 楼jishiping(JSP 季世平)回复于 2002-04-02 22:51:37 得分 10
光看上面的代码是没有问题的,关键是 TBug 是什么东西,以及在Form的
OnPaint()事件中调用 tmpBug->Show() 是否有问题。Top
3 楼huguangtao(胡广涛)回复于 2002-04-02 23:11:19 得分 10
先后次序的问题。呵Top
4 楼lipai(CB青年)回复于 2002-04-02 23:19:45 得分 0
to huguangtao(胡广涛):你说的是什么次序问题,请讲清楚一点好吗?这是全部的代码
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TList *BugList;
class TBug:public TObject
{
private:
Graphics::TBitmap *FPic;
int FX;
int FY;
TCanvas *FCanvas;
public:
__fastcall TBug(int AX,int AY,AnsiString FileName,TCanvas *ACanvas);
void __fastcall Move(int Dir);
void __fastcall Show();
__property int Y={read=FY,write=FY};
__property int X={read=FX,write=FX};
};
void __fastcall TBug::Show()
{
FCanvas->Draw(FX,FY,FPic);
}
__fastcall TBug::TBug(int AX,int AY,AnsiString FileName,TCanvas *ACanvas)
{
FX=AX;FY=AY;FPic=new Graphics::TBitmap();
FPic->LoadFromFile(FileName);
FCanvas=ACanvas;
Show();
}
void __fastcall TBug::Move(int Dir)
{
switch(Dir){
case 1://up
FY-=3;
break;
case 2://down
FY+=3;
break;
case 3://left
FX-=3;
break;
case 4://right
FX+=3;
break;
}
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{//添加元素
BugList->Add(new TBug(random(300),random(300),"live.bmp",Canvas));
ListBox1->Items->Add("A Bug");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{//删除元素
if(ListBox1->ItemIndex>=0){
TBug *tempBug=(TBug *)BugList->Items[ListBox1->ItemIndex];
delete tempBug;
BugList->Delete(ListBox1->ItemIndex);
ListBox1->Items->Delete(ListBox1->ItemIndex);
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{ //清空列表
TBug *tmpBug;
for(int i=0;i<BugList->Count;i++)
{
tmpBug=(TBug *)BugList->Items[i];
delete tmpBug;
}
BugList->Clear();
ListBox1->Clear();
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{//重画窗体。这段程序通过BugList对象轻易的遍历了所有元素
TBug *tmpBug;
for(int i=0;i<BugList->Count;i++)
{
tmpBug=(TBug *)BugList->Items[i];
tmpBug->Show();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton4Click(TObject *Sender)
{//控制一个对象上移
if(ListBox1->ItemIndex>=0){
TBug *tmpBug=(TBug *)BugList->Items[ListBox1->ItemIndex];
tmpBug->Move(1);
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
if(ListBox1->ItemIndex>=0){
TBug *tmpBug=(TBug *)BugList->Items[ListBox1->ItemIndex];
tmpBug->Move(2);
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
if(ListBox1->ItemIndex>=0){
TBug *tmpBug=(TBug *)BugList->Items[ListBox1->ItemIndex];
tmpBug->Move(3);
Invalidate();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if(ListBox1->ItemIndex>=0){
TBug *tmpBug=(TBug *)BugList->Items[ListBox1->ItemIndex];
tmpBug->Move(4);
Invalidate();
}
}Top
5 楼kingcaiyao(aking)回复于 2002-04-03 08:28:00 得分 5
这样看看:
TBug *tmpBug=dynamic_cast<TBug*>BugList->Items[ListBox1->ItemIndex];
Top
6 楼piaorenqian(湖心孤舟)回复于 2002-04-03 09:20:25 得分 10
问题出在FormPaint中,你的BugList是在Button1Click中构造的,而FormPaint时BugList还不存在拉Top
7 楼piaorenqian(湖心孤舟)回复于 2002-04-03 09:25:54 得分 0
TList *BugList;只是定义的一个指针变量
FormPaint时BugList中还不存在元素。Top
8 楼piaorenqian(湖心孤舟)回复于 2002-04-03 09:29:07 得分 0
Form在show之前要paint的Top
9 楼ALNG(?)回复于 2002-04-03 09:50:40 得分 5
从逻辑上讲,BugList作为类TForm1的成员变量更合理,而且要记得在TForm1的Ctor中为BugList分配地址,并在Dtor中进行删除。
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
BugList = new TList;
}
__fastcall TForm1::~TForm1(TComponent* Owner)
: TForm(Owner)
{
//Incase there are Bug object contained.
while(BugList->Count){
TBug * b=reinterpret_cast<TBug*>(BugList->First());
delete b;
//BugList->Remove(b);
BugList->Delete(0);
}
delete BugList;
BugList = NULL; //It's a good practice to NULL a deleted pointer.
}
Top




