1

I have a class Board which contains a pointer array of pointers, called Space** bo, each of the pointer points to an instance of the Space class. I have a derived class called SnakesAndLaddersSpace which inherits from Space, and SnakesAndLaddersBoard which inherits from Board.

Essentially , in my SnakesAndLaddersBoard class I want to change the Space** bo, which it inherits, to SnakesAndLaddersSpace** bo, how would I go about this?

This is what I'm doing in the SnakesAndLaddersBoard Class

SnakesAndLaddersBoard::Board::Board(int w, int h)
{ 
    width=w;
    height=h;//set up the size of the board

    bo = new SnakesAndLaddersSpace*[height];  // <<-- error
    for(int i = 0; i < height; i++)
    {
        bo[i] = new SnakesAndLaddersSpace[width]; 
    }
}

And here's the error I'm getting

SnakesAndLaddersBoard.cpp|13|error: invalid conversion from ‘SnakesAndLaddersSpace*’ to ‘Space*’ [-fpermissive]|

Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74

2 Answers2

6

That is because SnakesAndLaddersSpace** is not compatible with Space**, they are just unrelated due to too high level of indirection. Of cause, you still can assign pointer to SnakesAndLaddersSpace to the pointer to Space type, but you cannot assign pointer to pointer to SnakesAndLaddersSpace to the pointer to pointer to Space.

bo = new Space*[height]; // look here!!!
for(int i = 0; i < height; i++)
{
    bo[i] = new SnakesAndLaddersSpace[width];
}
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
  • Thanks that seems to have solved the problem, the only other issue I'm having now though, is I don't seem to be able to call the methods in SnakesAndLaddersSpace as below http://pastebin.com/QQtfUp5W – user2327041 Apr 27 '13 at 15:18
  • @user2327041, Well you cannot call functions declared in derived class from the base class. In common case, you can declare pure virtual function in the base class and implement it in derived, but it does not look reasonable here. Otherwise, you can use `static_cast` to cast from base class to derived, if you are sure, that pointer to base class contains a pointer to derived. Or, may be you should rethink your architecture for clear solution. – Lol4t0 Apr 27 '13 at 18:25
-1

It doesn't make sense... If you are making a new variable then shouldn't you be calling the constructor? bo[i]= new SnakesAndLaddersSpace(width); ..?

Rabbiya Shahid
  • 422
  • 3
  • 14
  • 1
    Nope, in this case the OP is allocating `width` objects of type `SnakesAndLaddersSpace` and returning a pointer to the first one as a result. In your answer the code will only allocate one element using a constructor that receives an integer argument. A very different thing as you can see. – Daniel Martín Apr 27 '13 at 15:03