Custom Key for std::map<> [duplicate]

2019-07-11 03:15发布

This question already has an answer here:

I am trying to use the following struct as a custom key for 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;
        } 
};

The following dictionary is a member of a separate InputManager class:

std::map<ActionId, std::set<sf::Keyboard::Key>> _keyBindings;

which is accessed in this member function:

std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const {
    return _keyBindings[action];
}

Unfortunately, the function is throwing this compiler error:

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)

According to this article, the operator<() member of ActionId with const qualification should be sufficient to make it a custom map key, while this article says that all I need is to make ActionId copiable and assignable. Clearly, my struct meets both of these criteria, so why won't InputManager::keysBoundTo() compile?

1条回答
三岁会撩人
2楼-- · 2019-07-11 03:53

The index operator (the "[]") is a non-const member function of std::map. Whereas, you have clearly indicated that keysBoundTo is a const member.

Rewrite keysBoundTo as

std::set<sf::Keyboard::Key> InputManager::keysBoundTo(ActionId action) const
{
    auto it = keyBindigs_.find(action);
    if ( it == keyBindings_.end() )
        return std::set<sf::Keyboard::Key>();
    else
        return it->second;
}

Notice that I renamed your member variable to have a trailing underscore. Do not use identifiers with leading underscores.

查看更多
登录 后发表回答