lal*_*rde 0 c++ unordered-map constants
我有与here、here或here相同的问题,除了我为参数和函数设置了const:
#include <unordered_map>
#include <functional>
namespace zzz {
struct identity_t {
static const int ID_SIZE = 5;
static const int LSB = 4; // = ID_SIZE - 1
unsigned char id[ID_SIZE];
inline bool operator< (const identity_t& rhs) const {
for (int i = 0; i < ID_SIZE; i++) if (id[i] != rhs.id[i]) return (id[i] < rhs.id[i]);
return false; // equal
}
unsigned char operator[] (const int& i) {return id[i];}
};
class hash_identity_t {
public:
long operator()(const identity_t& x) const {
return (long) x[identity_t::LSB];
}
};
class equal_to_identity_t {
public:
bool operator()(const identity_t& lhs, const identity_t& rhs) const {
for (int i = 0; i < identity_t::ID_SIZE; i++) if (lhs[i] != rhs[i]) return false;
return true;
}
};
Run Code Online (Sandbox Code Playgroud)
但我有同样的编译错误:
error: passing 'const zzz::identity_t' as 'this' argument of 'unsigned char zzz::identity_t::operator[](const int&)' discards qualifiers [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
您正在尝试对function中的参数调用非constfunction( identity_t::operator[]) 。constconstlong hash_identity_t::operator( const identity_t& x )
使identity_t::operator[]恒定。
//--------------------------------------vvvvv
unsigned char operator[] (const int& i) const {return id[i];}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12284 次 |
| 最近记录: |