0

I'm having issue with initialising a vector in one of my derived classes. I'm using OGRE and want to initialise a position in a derived class called CMissile.

CMissile inherits from CWeapon (which has one pure virtual function).

CWeapon.h:

#include "CPlayer.h"

class CWeapon
{
protected:
    CPlayer& myOwner; //Reference to player
    Ogre::Vector3 myPosition;

public:
    CPlayer getOwner();
    virtual void doBehaviour() = 0; //Do not add method body for this in CWeapon.cpp, pure virtual function
};

CMissile.h:

#include "CWeapon.h"

class CMissile : CWeapon
{
private:
    float myDirection;

public:
    CMissile(float, float, float, float, CPlayer&);
};

and here in CMissile.cpp is where my error resides:

#include "CMissile.h"

CMissile::CMissile(float x, float y, float z, float dir, CPlayer& player)
{
    this->myOwner = player;
    this->myDirection = dir;
    this->myPosition = new Ogre::Vector3(x, y, z); //here is the error, which reads "No operator '=' matches these operands"
}

In CPlayer.h (included in CWeapon) I have the line:

#include <OgreVector3.h>

Does anyone know what I'm doing wrong?

Adam
  • 311
  • 1
  • 3
  • 15
  • Could it be because Ogre::Vector3 myPosition; is protected? – Aidan Jan 15 '14 at 00:48
  • Also note that this->myOwner=player; will NOT change what mOwner is referring to, but instead overwrite whatever it was pointing to (nothing?!) with the contents of "player". No idea why you do not get a compile error for not initializing it - did you leave that out? – ltjax Jan 15 '14 at 13:41

1 Answers1

4

new Ogre::Vector3 will allocate a new vector on the heap (resulting in a Ogre::Vector3 *, a pointer to that vector). You are trying to assign it to myPosition, which is simply of type Ogre::Vector3. Those two types are not compatible.

You probably don't want to use new at all here, and instead do:

this->myPosition = Ogre::Vector3(x, y, z);

(which will assign a temporary vector to myPosition) or just directly update the position via:

this->myPosition.x = x;
this->myPosition.y = y;
this->myPosition.z = z;
  • Ahh, I've been working with JMonkey all week up until now, and am in the habit of having to initialise vectors as new objects before using them. – Adam Jan 15 '14 at 00:55