0

I have a function void MOVE_TO(Square **sendingSquare, Square **receivingSquare), where Square is a class:

class Square;
class Entity
{    
public:
    Square *currentSq;
};//  Just a data type--pretend it's your favorite class.
class Square
{
public:
    Entity *occupant;
};

void MOVE_TO(Square **sendingSquare, Square **receivingSquare)
{
    Entity *movingOccupant = (*sendingSquare)->occupant;
    (*receivingSquare)->occupant = movingOccupant;
    movingOccupant->currentSq = *receivingSquare;
    (*sendingSquare)->occupant = NULL;
}

problem is, when MOVE_TO(...) returns, both squares are then pointing at the receiving square, and the occupant that was supposed to be moved has disappeared altogether. Idea's/suggestions? My code is pretty much stuck until I can get around this. If I figure out my problem before any suggestions come in, I'll come back and answer my own question.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
iiian
  • 393
  • 1
  • 5
  • 17
  • 1
    It is totally unclear why you use `Square **` pointers instead of simple `Square *` pointers as `MOVE_TO` parameters, but that can still work, if done properly. And it should work in its current form. I don't see anything inside `MOVE_TO` that would produce the strange behavior you describe. Anyway, where's the call to `MOVE_TO`? How do you call it? – AnT stands with Russia Dec 05 '12 at 04:16
  • I would change the casing on `MOVE_TO`. It looks like a macro, as macros are typically all caps so they stand out as macros. – chris Dec 05 '12 at 04:32
  • @AndreyT I'm going to try using single pointers and see what that does. – iiian Dec 05 '12 at 04:33
  • @Chris I grouped it in with a bunch of enums that describe entity movement behavior. – iiian Dec 05 '12 at 04:34
  • Can we see the surrounding code which is calling MOVE_TO, including the full scope of the variables you are using to store your squares and entity? – JBentley Dec 05 '12 at 04:41
  • You have to tell us what's your purpose for MOVE_TO function, normally it is not encouraged to swap pointers like this, either you are coding in pure C style, or there's something wrong. – tomriddle_1234 Dec 05 '12 at 04:57

1 Answers1

0

I assumed you want to move the instance of an entity from sendingSquare to receivingSquare, and adjust the currentSq of the moved entity so that it rever to receivingSquare.

if that is the case, the code below will do that:

#include <iostream>
using namespace std;

class Square;
class Entity
{    
public:
    Square *currentSq;
    Entity(): currentSq(NULL){}//set currentSq to NULL using constructor initialization list
};

class Square
{
public:
    Entity *occupant;
    Square(): occupant(NULL){}//set occupant to NULL using constructor initialization list
};

//MOVE_TO with single '*'
void MOVE_TO(Square *sendingSquare, Square *receivingSquare)
{
    Entity *movingOccupant = sendingSquare->occupant;
    receivingSquare->occupant = movingOccupant;
    movingOccupant->currentSq = receivingSquare;
    sendingSquare->occupant = NULL;
}

int main(int argc, char** argv) {
    //create instances
    Square *sendingSquare = new Square(), *receivingSquare = new Square();
    Entity *entity = new Entity();

    //set up instances accordingly
    sendingSquare->occupant = entity;
    entity->currentSq = sendingSquare;

    //print instances address before MOVE_TO invoked
    //we know that receivingSquare.occupant is NULL, printing receivingSquare.occpuant.currentSq is commented
    cout << "sendingSquare: "<< sendingSquare 
         << ", sendingSquare.occupant: " << sendingSquare->occupant
         << ", sendingSquare.occupant.currentSq: " <<  sendingSquare->occupant->currentSq
         << ", receivingSquare: " <<receivingSquare 
         << ", receivingSquare.occupant: " << receivingSquare->occupant 
         //<< ", sendingSquare.occupant.currentSq: " << receivingSquare.occupant->currentSq
         << endl;

    MOVE_TO(sendingSquare,receivingSquare);

    //print instances address afer MOVE_TO invoked
    //we know that sendingSquare.occupant is NULL, printing sendingSquare.occpuant.currentSq is commented 
    cout << "sendingSquare: "<< sendingSquare 
         << ", sendingSquare.occupant: " << sendingSquare->occupant
         //<< ", sendingSquare.occupant.currentSq: " <<  sendingSquare.occupant->currentSq
         << ", receivingSquare: " << receivingSquare 
         << ", receivingSquare.occupant: " << receivingSquare->occupant 
         << ", receivingSquare.occupant.currentSq: " << receivingSquare->occupant->currentSq
         << endl;

    //commenting instance deletion. The program is ended anyway
    //delete entity,sendingSquare,receivingSquare;
    return 0;
}
Riza
  • 1,144
  • 8
  • 19