I am writing a program where one of my classes(called Dungeon) have a pointer to some class(called Player).
my first class returns this pointer to yet another class(called Game), and when I try to follow this pointer, it says that “No members available”
I am not sure which part of the code I should put here, since there is a lot of code in my project, but I will try to do my best.
Dungeon.h file have a private member declared like this
Player * m_player;
And a public function declared like this
Player* player() const;
The implementation of this function and function called addPlayer is as followed
bool Dungeon::addPlayer(int r, int c)
{
if (m_player != nullptr)
return false;
else
{
m_player=new Player(this,r,c);
return true;
}
}
Player* Dungeon::player() const
{
return m_player;
}
Note that in Dungeon class, m_player is initialized correctly, since I can access
member functions of Player class and everything seems to work ok.
in Game.h private part I declare pointer to Dungeon class like this
Dungeon* m_dungeon;
And here is the functions that you need to know from Game.c file
Game::Game(int goblinSmellDistance)
{
m_dungeon= new Dungeon();
//Some code that there is no need to show
m_dungeon=new Dungeon();
int r=0;
int c=0;
m_dungeon->returnRandomAvailableCoord(r,c); //does not matter what returns
m_dungeon->addPlayer(r,c);
}
Game::~Game()
{
}
void Game::play()
{
Player* p=m_dungeon->player();
//Here is the problem. when I am trying to follow p to access member functions of Player class, it says that there is no members available.
//Some more irrelevant code here
}
So Basically in Dungeon class, I am able to access the functons of Player class
trough m_player pointer, but when I try to do the same in Game class, it says no members available
Source: c++