There are a few problems with your code:
m_weapon(Weapon(this))
When you do it, you're trying to construct a new Weapon
object. But at this stage, it has not been defined yet (only forward declared). Try moving the constructor's implementation to your .cpp file, where you have included the Weapon.h
file.
Anothre problem is that m_weapon
is a reference to a Weapon
object. When you do m_weapon(Weapon(this))
, you're trying to contruct a new object, pass a reference to it and immediately destroy it, as it's a temporary. You can't do that.
What you can do is change m_weapon
to be a pointer and initialise it with new Weapon(this)
. You then have to remember to destroy it in Player's destructor.
I this scenario it is important to establish which object owns the other. I this case Player would own the Weapon object and it would be it's duty to manage it (delete when appropriate).