TList
保持无类型void*
指针每行的错误。它的Items[]
属性获取器返回一个void*
指针。您需要才能访问您的数据字段类型强制转换:
// DO NOT use the OnCreate event in C++! Use the actual constructor instead...
__fastcall TDogReportForm::TDogReportForm(TComponent *Owner)
: TForm(Owner)
{
DogListView->Items->Clear();
for (int i = 0; i < DogList->Count; i++)
{
// use whatever your real type name is...
MyDogInfo *Dog = static_cast<MyDogInfo*>(DogList->Items[i]); // <-- type-cast needed!
TListItem *Item = DogListView->Items->Add();
Item->Caption = Dog->firstName;
Item->SubItems->Add(Dog->lastName);
Item->SubItems->Add(Dog->ownerName);
Item->SubItems->Add(Dog->hours);
Item->SubItems->Add(Dog->dogNum);
}
}
在一个侧面说明,而不是所有的狗的信息复制到TListView
,你可能会考虑在虚拟模式下使用TListView
(组OwnerData
为true,分配OnData
事件处理程序),所以它可以在需要时从DogList
点播直接显示信息:
__fastcall TDogReportForm::TDogReportForm(TComponent *Owner)
: TForm(Owner)
{
DogListView->Items->Count = DogList->Count;
}
void __fastcall TDogReportForm::DogListViewData(TObject *Sender, TListItem *Item)
{
// use whatever your real type name is...
MyDogInfo *Dog = static_cast<MyDogInfo*>(DogList->Items[Item->Index]);
Item->Caption = Dog->firstName;
Item->SubItems->Add(Dog->lastName);
Item->SubItems->Add(Dog->ownerName);
Item->SubItems->Add(Dog->hours);
Item->SubItems->Add(Dog->dogNum);
}
虽这么说,你应该改变DogList
使用一个不同的容器,它更多的类型 - 安全然后TList
,如std::vector
:
std::vector<MyDogInfo> DogList;
...
MyDogInfo &Dog = DogList[index]; // <-- no type-cast needed
Item->Caption = Dog.firstName;
Item->SubItems->Add(Dog.lastName);
Item->SubItems->Add(Dog.ownerName);
Item->SubItems->Add(Dog.hours);
Item->SubItems->Add(Dog.dogNum);
那么'Items'是一个指针数组?如果不是,那是你的问题。 –
'DogList'没有在您的代码块中声明。它在哪里声明,它是什么类型? – nephtes