我正在尝试使用多键结构作为键来创建多重映射,但出现如下错误:
代码:
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/