3

そのため、構造体を作成して演算子TileSetをオーバーライドし、優先キューに入れようとしています。const 参照で非 const メソッドを呼び出すことはできないと読みましたが、実際には問題はないはずです。メンバーを変更するのではなく、メンバーにアクセスしているだけです。<TileSet

    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 に変更してもうまくいきませんでした。とにかく非 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;
}

これは、関数に渡されたパラメーターが const であることをコンパイラーに伝えます。それ以外の場合、const 参照をパラメーターthisとして渡すことはできません。this

于 2013-05-08T01:59:11.197 に答える
0

operator< を const メンバー関数にしてみてください。

于 2013-05-08T02:04:24.290 に答える