有關TList的錯誤,請大家幫忙看… |
答題得分者是:william
|
danielj
初階會員 發表:65 回覆:135 積分:40 註冊:2003-06-11 發送簡訊給我 |
小弟想將某一組data structure記錄在TList中以供後面程式取用某個data structure內容,但是不知道怎麼寫才對,我初步先把structure內容載入MemoryStream中,然後再將MemoryStream加入TList中(因為我只成功加入過MemoryStream到TList中),程式碼如下: typedef struct
{
AnsiString Name ;
int Size ;
} myStruct ; void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *tmp = new TMemoryStream;
myStruct *data = new myStruct;
data->Name = "test1";
data->Size = 100;
TMemoryStream *stream = new TMemoryStream;
stream->Write(data,sizeof(myStruct));
mylist->Add(stream);
tmp = (TMemoryStream *)mylist->Items[0];
data1 = (myStruct *)tmp ;//預期data1的內容應該要與data一樣才對,但是沒有一樣
delete tmp;
delete stream;
delete data1;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
mylist = new TList ;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
delete mylist;
}
//---------------------------------------------------------------------------
//再加一段測試是否Global也有效
void __fastcall TForm1::Button2Click(TObject *Sender)
{
myStruct *data2 = new myStruct ;//小弟測試時執行到此會出現Access Violation錯誤
data2 = (myStruct *)mylist->Items[0];
Memo1->Lines->Add("Name" data2->Name);
Memo1->Lines->Add("Size" IntToStr(data2->Size));
}
//--------------------------------------------------------------------------- 發表人 - danielj 於 2003/07/25 11:33:57 發表人 - danielj 於 2003/07/25 11:38:35 發表人 - danielj 於 2003/07/25 11:58:31
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
This is a LAZY solution using ShortString < class="code">
typedef struct
{
ShortString Name ;
int Size ;
} myStruct ; void __fastcall TForm1::Button1Click(TObject *Sender)
{
myStruct *data = new myStruct;
data->Name = "test1";
data->Size = 100;
TMemoryStream *stream = new TMemoryStream;
stream->Write(data,sizeof(myStruct));
mylist->Add(stream);
delete data;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
mylist = new TList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
int i;
for (i=0;i
|
brook
資深會員 發表:57 回覆:323 積分:371 註冊:2002-07-12 發送簡訊給我 |
不知是不是你要的.
void __fastcall TForm1::Button1Click(TObject *Sender) { TMemoryStream *tmp = new TMemoryStream; myStruct *data = new myStruct; myStruct *data1; TList *mylist = new TList; data->Name = "test1"; data->Size = 100; TMemoryStream *stream = new TMemoryStream; stream->Write(data,sizeof(myStruct)); mylist->Add(stream->Memory); //tmp = (TMemoryStream *)mylist->Items[0]; //data1 = (myStruct *)tmp ;//預期data1的內容應該要與data一樣才對,但是沒有一樣 data1 = (myStruct *)mylist->Items[0]; // 如此就一樣了 delete mylist; delete tmp; delete stream; delete data1; } |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |