c++ - multimap 运算符函数错误

标签 c++ qt stl

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

代码:

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

    char InstrumentName[6];
    char Symbol[10];
    long long ExpiryDate;
}
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;
       }
    };
std::multimap<MultiKey, stCont,myComp> cont_map;

错误:

expression having type 'const myComp' would lose some const-volatile qualifiers in order to call 'bool myComp::operator ()(const MultiKey &,const MultiKey &)'

最佳答案

你应该像这样重写多映射代码并删除 mycomp 结构:

struct MultiKey {

    char InstrumentName[6];
    char Symbol[10];
    long long ExpiryDate;


    bool operator< (const MultiKey& lhs)const
    {
        if((lhs.ExpiryDate==ExpiryDate)&&(memcmp(lhs.InstrumentName,InstrumentName,6))&&(memcmp(lhs.Symbol,Symbol,10)))
                   {
                       return true;
                   }

                   return false;
    }


};

关于c++ - multimap 运算符函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18655966/

相关文章:

python - 赛通 "Cannot take address of memoryview slice"

c++ - 在 C++ 中为多项式重载 i/o 运算符

c++ - 如何在不丢失 OOP 的情况下使对象在内存中连续?

c++ - 在 Qt Creator 中的 Release模式下设置断点

c++ - c++ 集合中的用户定义数据类型

c++ - exe的基地址?

c++ - Qt 如何终止使用 startTimer() API 启动的计时器?

Qt 创建者 "Cannot create file (...) qtcreator.xml"(Mac)

c++ - 我如何知道一个 STL 对象占用了多少内存?

c++ - 如何创建具有未知成员函数的通用容器?