1
class Board{
private:
    Shape shapes[100];
    Tile* tiles[16];
public:
    const Shape (&getShapes() const)[100]{return shapes;}; // (1) 
    const Tile* (&getTiles() const)[16]{return tiles;}; // (2)
};

I made this class called Board that has two methods returning an array by reference.

Method (2) reports an error:

qualifiers dropped in binding reference of type "const Tile *(&)[16]" to initializer of type "Tile *const [16]"

I fixed this error by writing const to the return type in method (1), but it doesn't work for method (2).

Why is this error occurring?

2
  • 4
    Use std::array and all your problems go away. Commented Nov 8, 2021 at 16:50
  • 1
    You const qualified the pointed-to type, but you need to const qualify the pointer type. Tile* const (&getTiles() const)[16]{return tiles;};. const to Tile is possible, but optional. Commented Nov 8, 2021 at 16:54

1 Answer 1

2

The element type of this array

 Tile* tiles[16]

is Tile *. As the member function is a constant member function then the function should return the array by reference with constant elements. That is it should be declared like

Tile* const (&getTiles() const)[16]{return tiles;}

That is you may not assign new values to the pointers stored in the array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.