1

Hello guys i have a logical problem...i had to do a kinda fo labyrinth and it work but then im trying to do it with classes...so here it is.

i have a function outside of main called void playeraction();

cout<<"\nAction : ";
cin>>action;

int prevX=posX;
int prevY=posY;
unsigned char space = {32};

switch (action)
{
    case 'a':
        if(grid[posX][posY-1]!='#')
        {
            posY--;
            grid[prevX][prevY]=space;
            system("cls");
            break;
        }

when its like this the character moves without any problem now when i try to implimenet classes it doesnt

    case 's':

        if(grid[posX+1][posY]!='#')
        {
            Dragon obj;
            obj.moveSouth(posX);
            grid[prevX][prevY]=space;
            system("cls");
            break;
        }

in dragon cpp

int Dragon::moveSouth(int posX)
{
    return posX++;
}

any ideas why it doesn't return the posX++??

wkl
  • 77,184
  • 16
  • 165
  • 176
RedFox
  • 85
  • 2
  • 3
  • 9

3 Answers3

1

In your code, there are some bugs.

First, for 'posX++', posX is increased after using its value. You should use ++posX to make it be increased before using.

Second, in the function, the memory of its arguments is on stack. They are temporary, not origin memory of the variables.

You can use reference for this code.

void Dragon::moveSouth(int & posX){

    posX++;

}
Yuan
  • 1,147
  • 11
  • 16
  • I use reference and it worked :) im gonna try the other variants that guys suggested thanks everyone for the help :) – RedFox Dec 29 '12 at 15:52
0

Because you are not assigning the returned value to any variable. Try this:

int newSouth = obj.moveSouth(posX);

This will assign to newSouth the new posX value.

EDIT: You have to also modify your moveSouth function, as Mats Petersson mentions in the first comment.

int Dragon::moveSouth(int posX){

 int newX = posX++;
 return newX;

}

However, if rather than returing a new value you wanted to increase the original posX, just use a reference to posX, as mentioned by Yuan in the other answer.

Dan
  • 1,466
  • 1
  • 13
  • 27
  • 2
    Actually, that won't work either, as it's returning the value BEFORE increment, then incrementing the local variable that is disappearing 4 nanoseconds later. Either return posX + 1, or update the variable itself by passing it as a reference. – Mats Petersson Dec 29 '12 at 15:22
0

your function does not modify any sort of internal or external variable, it simply returns a value that is never caught. There are a number of solutions to this,

  • Create internal class variables to keep track of position, only calling a function to modify the position on the map.

class dragon
{
    //...
    void moveDragon(int num, int num2);
    private:
        int posX;
        int posY;
}

void dragon::moveDragon(int num, int num2)
{
    posX -= num;
    posY -= num2;
}
  • Have the function return the same value subtracted for the function

posX = obj.MoveSouth(posX); //new posX now holds the value
Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177