c++ - 将结构作为多映射中的键传递时出错

标签 c++ qt stl

我正在尝试使用多键结构作为键来创建多重映射,但出现如下错误:

代码:

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;

关于c++ - 将结构作为多映射中的键传递时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18653206/

相关文章:

c++ - 如何将用户定义的类用作模板类的一种类型,并正确地为这两个类重载 <<?

c++ - 有没有办法告诉程序正在抛出异常?

c++ - 在已编译的 C 或 C++ 代码中加密密码

c++ - COM:使用指向它实现的接口(interface)的指针获取 coclass 对象的 GUID

c++ - 在构造函数或静态中创建时qtimer崩溃

c++ - 遍历抽象类的 vector

c++ - 在两个 cpp 文件中包含一个函数的匿名命名空间

qt - 如何通过 QTcpSocket 发送整数?

c++ - QT 获取 DateTime 的 jpeg exif 元数据

C++ STL make_heap 和 pop_heap 不工作