尝试绘制精灵地图时出现sfml错误

时间:2013-12-27 23:14:50

标签: c++ map sfml

我有一个创建平铺地图的类,但我的虚拟空白绘制功能出错:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= getTransform();

        for (int k = 0; k < m_layers ; ++k)
        {
            for (int i = minimum.x; i < maximum.x && i < m_width; ++i)
            {
                for (int j = minimum.y; j < maximum.y && j < m_height; ++j)
                {
                    target.draw(m_map[m_width*(j+k*m_height)+(i+k*m_width)].block, states);
                }
            }
        }
    }

我有这个错误:

error: passing 'const std::map<int, carte>' as 'this' argument of 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = carte; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, carte> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = carte; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]' discards qualifiers [-fpermissive]|

我不知道如何解决这个问题,请帮助我:(

如果你想知道什么是“carte”意味着它是法语中的“地图”。 carte结构有3个成员:int(数据),sprite(“block”)和bool(用于碰撞)

抱歉我的英语很差。

2 个答案:

答案 0 :(得分:3)

您的绘制函数已声明为const。这意味着您不能更改它所调用的对象。您的地图是此对象的一部分,因此也不允许更改。现在你想知道这意味着什么,因为你的地图不会改变,对吧?好吧,编译器不知道这一点。您在地图上调用operator[]并将其声明为非const。换句话说:你在地图上调用了一个方法可能更改它,而你的编译器抱怨你不允许这样做。

const sf::Drawable& d = m_map.at(m_width*(j+k*m_height)+(i+k*m_width)).block;
target.draw(d, states);

这将在地图上调用保证不会更改的方法。

答案 1 :(得分:1)

error: passing 'const std::map<int, carte>'

您的地图似乎是const,因此您无法在其上使用operator[](例如,请参阅this问题)。您有两种选择:删除常量,或使用std::map::at方法访问元素。

相关问题