CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C++ Builder >  基础类

各位大师帮忙看看....

楼主lipai(CB青年)2002-04-02 22:40:34 在 C++ Builder / 基础类 提问

我照书上敲了一段代码,是关于用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

相关问题

  • 各位大师兄帮忙啦!!
  • 请各位大师帮帮忙!!!
  • 各位大师指点一下
  • 请教各位大师如何用delphi制作超宽报表?
  • 各位大师一个奇怪的现象出现了!!!!!!Help me!
  • 各位高手,程序员,大师..救救我,,
  • 简单问题,关于数据窗口,请教各位大师!
  • 求助各位大师:EPrivilege的错误,急!!
  • 请教各位大师一个问题,谢谢了!!!
  • 请问这是什么原因?望各位大师指点

关键词

  • 代码
  • buglist
  • tbug
  • tmpbug
  • fastcall
  • tform
  • formpaint
  • fy
  • fx
  • itemindex

得分解答快速导航

  • 帖主:lipai
  • invalid
  • jishiping
  • huguangtao
  • kingcaiyao
  • piaorenqian
  • ALNG

相关链接

  • CSDN Blog
  • 技术文档
  • 代码下载
  • 第二书店
  • 读书频道

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo