C++ - 散列用户定义的对象时出现类型限定符错误
C++ - type qualifiers error when hashing user defined object
我目前有一个名为 ResultTableEntry 的用户定义 class,我希望能够创建一个 std::unordered_set。我发现可以为我的 class 创建一个哈希函数,以便我初始化集合。
#include <vector>
#include <string>
class ResultTableEntry {
private:
int relIndex;
std::vector<int> paramIndex;
std::vector<std::string> result;
public:
ResultTableEntry::ResultTableEntry(int, std::vector<int>, std::vector<std::string>);
int getRelationshipIndex();
std::vector<int> getParameterIndex();
std::vector<std::string> getResult();
};
namespace std {
template <>
struct hash<ResultTableEntry>
{
std::size_t operator()(const ResultTableEntry& k) const
{
size_t res = 17;
for (auto p : k.getParameterIndex()) {
res = res * 31 + hash<int>()(p);
}
for (auto r : k.getResult()) {
res = res * 31 + hash<std::string>()(r);
}
res = res * 31 + hash<int>()(k.getRelationshipIndex());
return res;
}
};
}
我根据以下内容实现了我的哈希函数:C++ unordered_map using a custom class type as the key
然而,我一直面临这些错误。
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getParameterIndex"
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getResult"
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getRelationshipIndex"
- 具有类型 'const std::hash' 的表达式将丢失一些 const-volatile 限定符以调用 'size_t std::hash::operator ()(ResultTableEntry &)'
删除参数中的 'const' 似乎也无济于事。我的实施有问题吗?我将无法使用其他库,例如 boost。
你需要向编译器保证成员函数不会修改*this指针
#include <vector>
#include <string>
class ResultTableEntry {
private:
int relIndex;
std::vector<int> paramIndex;
std::vector<std::string> result;
public:
ResultTableEntry(int, std::vector<int>, std::vector<std::string>);
int getRelationshipIndex() const;
std::vector<int> getParameterIndex() const;
std::vector<std::string> getResult() const;
};
namespace std {
template <>
struct hash<ResultTableEntry>
{
std::size_t operator()(const ResultTableEntry& k) const
{
size_t res = 17;
for (auto p : k.getParameterIndex()) {
res = res * 31 + hash<int>()(p);
}
for (auto r : k.getResult()) {
res = res * 31 + hash<std::string>()(r);
}
res = res * 31 + hash<int>()(k.getRelationshipIndex());
return res;
}
};
}
我目前有一个名为 ResultTableEntry 的用户定义 class,我希望能够创建一个 std::unordered_set。我发现可以为我的 class 创建一个哈希函数,以便我初始化集合。
#include <vector>
#include <string>
class ResultTableEntry {
private:
int relIndex;
std::vector<int> paramIndex;
std::vector<std::string> result;
public:
ResultTableEntry::ResultTableEntry(int, std::vector<int>, std::vector<std::string>);
int getRelationshipIndex();
std::vector<int> getParameterIndex();
std::vector<std::string> getResult();
};
namespace std {
template <>
struct hash<ResultTableEntry>
{
std::size_t operator()(const ResultTableEntry& k) const
{
size_t res = 17;
for (auto p : k.getParameterIndex()) {
res = res * 31 + hash<int>()(p);
}
for (auto r : k.getResult()) {
res = res * 31 + hash<std::string>()(r);
}
res = res * 31 + hash<int>()(k.getRelationshipIndex());
return res;
}
};
}
我根据以下内容实现了我的哈希函数:C++ unordered_map using a custom class type as the key
然而,我一直面临这些错误。
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getParameterIndex"
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getResult"
- 该对象具有与成员函数不兼容的类型限定符"ResultTableEntry::getRelationshipIndex"
- 具有类型 'const std::hash' 的表达式将丢失一些 const-volatile 限定符以调用 'size_t std::hash::operator ()(ResultTableEntry &)'
删除参数中的 'const' 似乎也无济于事。我的实施有问题吗?我将无法使用其他库,例如 boost。
你需要向编译器保证成员函数不会修改*this指针
#include <vector>
#include <string>
class ResultTableEntry {
private:
int relIndex;
std::vector<int> paramIndex;
std::vector<std::string> result;
public:
ResultTableEntry(int, std::vector<int>, std::vector<std::string>);
int getRelationshipIndex() const;
std::vector<int> getParameterIndex() const;
std::vector<std::string> getResult() const;
};
namespace std {
template <>
struct hash<ResultTableEntry>
{
std::size_t operator()(const ResultTableEntry& k) const
{
size_t res = 17;
for (auto p : k.getParameterIndex()) {
res = res * 31 + hash<int>()(p);
}
for (auto r : k.getResult()) {
res = res * 31 + hash<std::string>()(r);
}
res = res * 31 + hash<int>()(k.getRelationshipIndex());
return res;
}
};
}