Sign in to follow this  
DanielDoyle

Why does a Derived class pointer to Base Class gives error C2440

Recommended Posts

DanielDoyle    102
The Problem I'm having is i get a error C2440: when i try to assign a pointer from a derived class to an instance of it's base class
error C2440: '=' : cannot convert from 'Camera *' to 'ThirdPersonCamera *'

The classes are:

Base Class:
[code]

class Camera : public Frustum
{
public:
Camera();
Camera(D3DXVECTOR3& Position, D3DXVECTOR3& lookAt);
Camera(D3DXVECTOR3& Position, D3DXVECTOR3& lookAt, D3DXVECTOR3& upPos);
Camera(D3DXVECTOR3& Position, D3DXVECTOR3& lookAt, D3DXVECTOR3& upPos, D3DXVECTOR3& rightPos);
~Camera();

void setViewMatrix(D3DXMATRIX& viewMat); //sets viewMatrix and bChanged to true
void setProjMatrix(D3DXMATRIX& projMat); //sets projMatrix and bChanged to true

D3DXMATRIX getViewMatrix();
D3DXMATRIX getProjMatrix();
bool hasChanged(); //return if state has changed

void updateCamera(LPDIRECT3DDEVICE9 gd3dDevice); //updates the engines view matrix to that of the cameras current state
void setPosition(float x, float y, float z);
void setLook(float x, float y, float z);
private:
protected:
D3DXMATRIX viewMatrix; //cameras view matrix (passed to the engine core)
D3DXMATRIX projMatrix; //cameras proj matrix (passed to the engine core);
bool bChanged; //stores Cameras state: false == camera still in same place, true == camera has changed

PositionData PreviousData; //data from last time camera was changed
//used to check if an actual pdate has occured
bool hasUpdated(); //checks and can be used to set update
};

[/code]
Derived Class
[code]

class ThirdPersonCamera : public Camera
{
public:
ThirdPersonCamera(AObject* ObjectToFollow, D3DXVECTOR3 offset);
~ThirdPersonCamera();

//used to attach the camera to an object
bool attachToObject(AObject* object, D3DXVECTOR3 offset);

/*void setPosition();
void setPosition(D3DXVECTOR3 offset);*/
private:
//object the camera is set to follow
AObject* attachedObject;
};

[/code]

The line that is throwing the error is:
[code]
TCamera = this->GetCameraPointer();
[/code]

All the GetCameraPointer() function does is:
[code]
Camera* Engine::GetCameraPointer()
{
return camera;
}
[/code]

TCamera is defined as ThirdPersonCamera* Camera

As far as i know that should work as TCamera's class type is derived from Camera unless im mistaken and i am doing something wrong?
Any Ideas?

Share this post


Link to post
Share on other sites
TheUnbeliever    963
You're not trying to cast from the derived class to the base class, you're trying to go the other way, which you can't do without a cast because you don't know that the concrete type of the object pointed to isn't some other derived class. GetCameraPointer returns a Camera*, which you're trying to assign to TCamera, which you say is a ThirdPersonCamera*.

Share this post


Link to post
Share on other sites
DanielDoyle    102
[quote name='TheUnbeliever' timestamp='1305761651' post='4812790']
You're not trying to cast from the derived class to the base class, you're trying to go the other way, which you can't do without a cast because you don't know that the concrete type of the object pointed to isn't some other derived class. GetCameraPointer returns a Camera*, which you're trying to assign to TCamera, which you say is a ThirdPersonCamera*.
[/quote]

Ahhh Thankyou very much sir! works fine now :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this