2016-06-13 105 views
-1

我不断收到错误:的Borland C++ ListView的错误

[BCC32 Error] DogReport.cpp(29): E2288 Pointer to structure required on left side of -> or ->*

在编译的时候。

我想填充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); 
    } 
} 

有对包含DogList->

+0

那么'Items'是一个指针数组?如果不是,那是你的问题。 –

+0

'DogList'没有在您的代码块中声明。它在哪里声明,它是什么类型? – nephtes

回答

1

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);