Domanda

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?

È stato utile?

Soluzione

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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top