所以,我正在尝试创建一个结构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;
}
};
Run Code Online (Sandbox Code Playgroud)
错误消息告诉我:传递'const TileSet' as 'this' argument of 'bool TileSet::operator<(const TileSet&)' discards qualifiers [-fpermissive]这是什么意思?将变量更改为const不起作用,无论如何我都需要它们是非const的.
我尝试这样做时会发生错误:
std::priority_queue<be::Object::TileSet> tileset_queue;
您需要在方法const定义中添加限定符operator<:
bool operator<(const TileSet &b) const
// ^^^ add me
{
return this->gid < b.gid;
}
Run Code Online (Sandbox Code Playgroud)
这告诉编译器this传递给函数的参数是const,否则它不允许您将const引用作为this参数传递.