<分区>
我正在尝试使用以下 struct
作为 std::map
的自定义键:
struct ActionId {
// ENCAPSULATED MEMBERS
private:
size_t _id;
static size_t _root;
static size_t incrementedRoot() {
return (_root += 1);
}
// INTERFACE
public:
ActionId() :
_id(incrementedRoot()) { }
ActionId(const ActionId& that) :
_id(that._id) { }
ActionId& operator=(const ActionId& that) {
this->_id = that._id;
return *this;
}
bool operator==(const ActionId& that) const {
return this->_id == that._id;
}
bool operator!=(const ActionId& that) const {
return this->_id != that._id;
}
bool operator<(const ActionId& that) const {
return this->_id < that._id;
}
};
以下字典是一个单独的InputManager
的成员类:
std::map<ActionId, std::set<sf::Keyboard::Key>> _keyBindings;
在此成员函数中访问:
std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const {
return _keyBindings[action];
}
不幸的是,该函数抛出此编译器错误:
error C2678: binary '[' : no operator found which takes a left-hand operand of type '
const std::map<Game2D::ActionId,std::set<sf::Keyboard::Key,std::less<_Kty>,std::allocator<_Kty>>,std::less<Game2D::ActionId>,std::allocator<std::pair<const Game2D::ActionId,_Ty>>>
' (or there is no acceptable conversion)
根据 this article , operator<()
ActionId
的成员与 const
资格应该足以使其成为自定义 map 键,而 this article说我只需要制作 ActionId
可复制和可分配。显然,我的结构符合这两个标准,那么为什么不 InputManager::keysBoundTo()
编译?