3

因此,我正在尝试创建一个结构TileSet并覆盖<运算符,然后将TileSet' 放入优先级队列中。我读过我不能在 const 引用上调用非 const 方法,但应该没有问题,我只是在访问成员,而不是更改它们:

    struct TileSet
    {

        // ... other struct stuff, the only stuff that matters

        TileSet(const TileSet& copy)
        {
            this->gid = copy.gid;
            this->spacing = copy.spacing;
            this->width = copy.width;
            this->height = copy.height;
            this->texture = copy.texture;
        }

        bool operator<(const TileSet &b)
        {
            return this->gid < b.gid;
        }
    }; 

错误信息告诉我:通过'const TileSet' as 'this' argument of 'bool TileSet::operator<(const TileSet&)' discards qualifiers [-fpermissive]是什么意思?将变量更改为 const 不起作用,无论如何我都需要它们是非常量的。

当我尝试这样做时会发生错误:

std::priority_queue<be::Object::TileSet> tileset_queue;

4

2 回答 2

6

您需要在方法const的定义中添加限定符operator<

bool operator<(const TileSet &b) const
                               // ^^^ add me
{
    return this->gid < b.gid;
}

这告诉编译器this传递给函数的参数是 const,否则将不允许您将 const 引用作为this参数传递。

于 2013-05-08T01:59:11.197 回答
0

尝试使 operator< 成为 const 成员函数。

于 2013-05-08T02:04:24.290 回答