Setter function not holding value

I have a character with x and y coordinates. When I set the coordinates in DungeonLevel, the values are gone by the time I try to get them in the main function. Any help would be much appreciated!


Here is my main function:

#include <iostream>
#include <random>
#include <ctime>
#include <string>

#include "DungeonLevel.h"
#include "Character.h"

using namespace std;

int main(int argc, char * argv[])
{
Character player;
mt19937 mt;
mt.seed( time(NULL) );
DungeonLevel dl(79, 20, mt, player);

// when i try to get these values, it says pxpos and pypos are 0
int pxpos = player.get_pxpos();
int pypos = player.get_pypos();
}

Here is my DungeonLevel.cpp:

DungeonLevel::DungeonLevel(int iWidth, int iHeight, std::mt19937 & mt, Character player)
{
// there is more code here, but it is irrelevant to my issue
//find random spot for player
int pxpos = mt() % iWidth;
while (pxpos == iWidth || pxpos == 0)
{
pxpos = mt() % iWidth;
}
int pypos = mt() & iHeight;
while (pypos == iHeight || pypos == 0)
{
pypos = mt() % iHeight;
}
while (m_vvTiles[pypos][pxpos] != '.')
{
pxpos = mt() % iWidth;
while (pxpos == iWidth || pxpos == 0)
{
pxpos = mt() % iWidth;
}
pypos = mt() % iHeight;
while (pypos == iHeight || pypos == 0)
{
pypos = mt() % iHeight;
}
}

// here, the positions actually do set
player.set_pxpos(pxpos);
player.set_pypos(pypos);
m_vvTiles[pypos][pxpos] = '@';
}

And here are the functions in Character.cpp
int Character::get_pxpos()
{
return iPlayerX;
}

void Character::set_pxpos(int pxpos)
{
iPlayerX = pxpos;
}

int Character::get_pypos()
{
return iPlayerY;
}

void Character::set_pypos(int pypos)
{
iPlayerY = pypos;
}

And then in Character.h:
virtual int get_pxpos();
virtual void set_pxpos(int pxpos);

virtual int get_pypos();
virtual void set_pypos(int pypos);


Let me know if you need more code. I kind of skimmed through it.
You need to pass a Character instance through to your DungeonLevel constructor as a reference:

 
DungeonLevel::DungeonLevel(int iWidth, int iHeight, std::mt19937 & mt, Character& player)


HTH
Oh thank you! I figured it was something along the lines of that!!
Topic archived. No new replies allowed.