我试图使用多键结构作为键来创建多图,但出现以下错误:

码:

struct stCont
{
    long long Tok;
    char Reserved;
    long long Asset;
}
struct MultiKey {

    char InstrumentName[6];
    char Symbol[10];
    long long ExpiryDate;
}
std::multimap<MultiKey, stCont> cont_map;

错误:
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xfunctional:125: error: C2678: binary '<' : no operator found which takes a left-hand operand of type 'const MultiKey' (or there is no acceptable conversion)
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qchar.h(391): could be 'bool operator <(QChar,QChar)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qbytearray.h(538): or       'bool operator <(const QByteArray &,const QByteArray &)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qbytearray.h(540): or       'bool operator <(const QByteArray &,const char *)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qbytearray.h(542): or       'bool operator <(const char *,const QByteArray &)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(565): or       'bool operator <(const QString &,const QString &)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(625): or       'bool operator <(const char *,const QString &)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(632): or       'bool operator <(const char *,const QStringRef &)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(975): or       'bool operator <(QLatin1String,QLatin1String)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(1032): or       'bool operator <(const char *,QLatin1String)' [found using argument-dependent lookup]
C:\Qt\Qt5.0.2\5.0.2\msvc2010\include\QtCore/qstring.h(1304): or       'bool operator <(const QStringRef &,const QStringRef &)' [found using argument-dependent lookup]
while trying to match the argument list '(const MultiKey, const MultiKey)'

我已经为myComp编写了以下代码:
struct myComp
    {
       bool operator() (const MultiKey& lhs, const MultiKey& rhs)
       {
           if((lhs.ExpiryDate==rhs.ExpiryDate)&&(memcmp(lhs.InstrumentName,rhs.InstrumentName,6))&&(memcmp(lhs.Symbol,rhs.Symbol,10)))
           {
               return 1;
           }

           return 0;
       }
    };

现在我得到一个错误:
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xtree:1546: error: C3848: expression having type 'const myComp' would lose some const-volatile qualifiers in order to call 'bool myComp::operator ()(const MultiKey &,const MultiKey &)'

最佳答案

因为您没有为map定义自定义比较器(比较函数)

根据您的ExpiryDate可能像这样

    struct myComp
    {
       bool operator() (const MultiKey& lhs, const MultiKey& rhs)
       {
           return lhs.ExpiryDate < rhs.ExpiryDate ;
       }
    };

然后使用:
std::multimap<MultiKey, stCont,myComp> cont_map;

08-26 19:50