Borland C ++ ListView错误

时间:2016-06-13 19:38:38

标签: c++ listview c++builder

我一直收到错误:

  

[BCC32错误] DogReport.cpp(29):E2288指向左侧所需结构的指针 - >或 - > *

尝试编译时。

我正在尝试使用由结构组成的Client client = Client.create(); WebResource webResource = client.resource("https://api.test.com/test/{id}") .queryParam("param1", "test1") .queryParam("param2", "test2"); 中的元素填充TListView

TList

每行包含void __fastcall TDogReportForm::FormCreate(TObject *Sender) { DogListView->Items->Clear(); for (int i = 0; i < DogList->Count; i++) { TListItem * Item; Item = DogListView->Items->Add(); Item->Caption = DogList->Items[i]->firstName; Item->SubItems->Add(DogList->Items[i]->lastName); Item->SubItems->Add(DogList->Items[i]->ownerName); Item->SubItems->Add(DogList->Items[i]->hours); Item->SubItems->Add(DogList->Items[i]->dogNum); } }

时出错

1 个答案:

答案 0 :(得分:1)

TList包含无类型void*指针。其Items[]属性getter返回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设置为true并指定{,而不是将所有狗信息复制到OwnerData。 {1}}事件处理程序)因此它可以在需要时直接从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