表达式必须具有 class 类型 - C++/CLI

Expression must have a class type - C++/CLI

所以我有这段代码,我的灵感来自 here, someone already had this type of error but this didn't fix it in my case ():

using namespace System::Collections;
using namespace System::Windows::Forms;
int ListViewColumnSorter::Compare(System::Object^ x, System::Object^ y)
{
    int compareResult;
    ListViewItem^ listviewX;
    ListViewItem^ listviewY;

    // Cast the objects to be compared to ListViewItem objects
    listviewX = static_cast<ListViewItem^>(x);
    listviewY = static_cast<ListViewItem^>(y);

        // Compare the two items
    compareResult = ObjectCompare->Compare(listviewX->SubItems[ColumnToSort].Text,
        listviewY->SubItems[ColumnToSort].Text);

    // Calculate correct return value based on object comparison
    if (OrderOfSort == System::Windows::Forms::SortOrder::Ascending)
    {
        // Ascending sort is selected, return normal result of compare operation
        return compareResult;
    }
    else if (OrderOfSort == System::Windows::Forms::SortOrder::Descending)
    {
        // Descending sort is selected, return negative result of compare operation
        return (-compareResult);
    }
    else
    {
        // Return '0' to indicate they are equal
        return 0;
    }
}

但是它在这一行给我一个错误:

compareResult = ObjectCompare->Compare(listviewX->SubItems[ColumnToSort].Text,
        listviewY->SubItems[ColumnToSort].Text);

告诉我 listviewXlistviewY 必须有一个 class 类型。

我不明白,因为我正确地指向了 ListViewItem 类型的对象。

有什么想法吗?

错误消息在某种程度上有点误导,因为它暗示问题出在实际的 listviewXlistviewY 变量(正如您所指出的, 'hatted' class 类型,或句柄)。

但是,ListViewItem class 的 SubItems 成员是 class 句柄数组 (不是原始 classes),所以你也需要 -> 运算符来处理它的两次出现(而不是 . 运算符)。

这就是您需要的代码:

compareResult = ObjectCompare->Compare(listviewX->SubItems[ColumnToSort]->Text,
        listviewY->SubItems[ColumnToSort]->Text);