如何将 wxObject 类型转换为 wxVariant

How to typecast wxObject to wxVariant?

本文关键字:wxVariant 类型转换 wxObject      更新时间:2023-10-16

我有一个wxObject的地图。但我想将其类型转换为 wxVariant。

void MWDataViewTable::InitColumnValues(wxString targetcolumn,wxString sourcecolumn , std::map<wxObject,wxObject> srctargetvalue)
{
    wxVariant srcvalue;
    wxVariant tgtvalue;
    int srccolumnpos = GetColumnPosition(sourcecolumn);
    int tgtcolumnpos = GetColumnPosition(targetcolumn);
    int rows = m_rowdataList.size()-1;  //without header
    for(int i=0;i< rows;i++)
    {       
        GetValue(srcvalue,i,srccolumnpos);
        tgtvalue = (wxVariant)srctargetvalue[srcvalue] ;// typecasting
        SetValue(tgtvalue,i,tgtcolumnpos/*toggle-column*/);
    }
}

在突出显示的行中,我正在进行类型转换。.但这给了我一个错误,上面写着"错误 1 错误 C2678:二进制'<':找不到采用类型为'const wxObject'的左操作数的运算符"此错误出现在 xstddef.h 文件中。我不知道为什么会发生这种情况,或者我是否错误地键入了它。请帮忙..!

std::map中,key values are generally used to sort and uniquely identify the elements .

在代码中,键和值都是wxObject类型。wxObject类似乎没有重载小于运算符的方法(我不知道这些wx对象是什么(。

std::map需要一种less operator方法来执行对键值进行排序所需的比较。因此,您应该将自己的比较函数传递给 std::map 来比较两个 wxObjects。

模板容器 std::map 将比较函数作为第三个条件。

template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map; 

比较是一个二进制谓词,在您的情况下具有以下定义:

bool MyCompare( const wxObject& , const wxObject&)
{
  \Compare logic that returns true or false
}

您可以拥有自己的地图,该地图将使用此比较方法:

typedef std::map<wxObject,wxObject,&MyCompare> MyMap;
MyMap srctargetvalue;