Hiya guys,
This problem I have... I just can't figure why it is giving me a compile error when it looks perfectly fine, as far my my understanding goes.
But basically I got a Vehicle class, Spacecraft class, and a Fightercraft class.
Vehicle is parent of Spacecraft, Spacecraft is parent of Fightercraft.
I have a variable that is a pointer to Vehicle class, but it is actually a Fightercraft class object.
When I go to use a method that I know it has access to that exists in the parent class it says that member is not a member of Vehicle.
I give you guys a sample of the code
//This code exist in my main application entry point
Vehicle *player = new Fightercraft( "player", &D3DDevice );
And my inheritance for the Vehicle, Spacecraft and Fightercraft is as follows
//----- VEHICLE DEFINITION SECTION -----//
#ifndef VEHICLE_H
#define VEHICLE_H
#include <d3d9.h>
#include <d3dx9.h>
class Vehicle
{
public:
Vehicle();
~Vehicle();
virtual void setID( char *ptrID ) = 0;
virtual char* getID() = 0;
protected:
char *id;
};
#endif
//------ VEHICLE IMPLEMENTATION SECTION -------//
#include "Vehicle.h"
Vehicle::Vehicle()
{
id = "id default value: Vehicle";
}
Vehicle::~Vehicle()
{
delete[] id;
}
//------- SPACECRAFT DEFINITION SECTION --------//
#ifndef SPACECRAFT_H
#define SPACECRAFT_H
#include <d3d9.h>
#include <d3dx9.h>
#include "Vehicle.h"
class Spacecraft: public Vehicle
{
public:
Spacecraft();
Spacecraft( char *ptrID );
Spacecraft( char *ptrID, IDirect3DDevice9 **D3DDevice );
~Spacecraft();
void setID( char *ptrID );
virtual void setMaterial( D3DMATERIAL9 mat ) = 0;
virtual void createMesh( IDirect3DDevice9 **D3DDevice ) = 0;
char* getID();
virtual ID3DXMesh* getMesh() = 0;
virtual D3DMATERIAL9 getMaterial() = 0;
protected:
ID3DXMesh* meshShip;
D3DMATERIAL9 materialShip;
const int fuselage;
};
#endif
//--------- SPACECRAFT IMPLEMENTATION SECTION --------
#include "Spacecraft.h"
Spacecraft::Spacecraft(): fuselage( 1 )
{
id = "id default value: Spacecraft";
}
Spacecraft::Spacecraft( char *ptrID ): fuselage( 1 )
{
id = ptrID;
}
Spacecraft::Spacecraft( char *ptrID, IDirect3DDevice9 **D3DDevice ): fuselage( 1 )
{
ZeroMemory( &materialShip, sizeof( D3DMATERIAL9 ));
materialShip.Diffuse.r = materialShip.Ambient.r = 1.0f;
materialShip.Diffuse.g = materialShip.Ambient.g = 1.0f;
materialShip.Diffuse.b = materialShip.Ambient.b = 1.0f;
materialShip.Specular.r = 0.4f;
materialShip.Specular.g = 0.4f;
materialShip.Specular.b = 0.4f;
materialShip.Power = 8.0f;
}
Spacecraft::~Spacecraft()
{
if( meshShip != NULL )
{
meshShip->Release();
meshShip = NULL;
}
}
void Spacecraft::setID( char *ptrID )
{
id = ptrID;
}
char* Spacecraft::getID()
{
return id;
}
//----------------- FIGHTERCRAFT DEFINITION SECTION ------
#ifndef FIGHTERCRAFT_H
#define FIGHTERCRAFT_H
#include <d3d9.h>
#include <d3dx9.h>
#include "Spacecraft.h"
#include "ShipWeaponCapability.h"
class Fightercraft: public Spacecraft
{
public:
Fightercraft();
Fightercraft( char *ptrID );
Fightercraft( char *ptrID, IDirect3DDevice9 **D3DDevice );
void setID( char *prtID );
void setMaterial( D3DMATERIAL9 mat );
void createMesh( IDirect3DDevice9 **D3DDevice );
char* getID();
ID3DXMesh* getMesh();
D3DMATERIAL9 getMaterial();
};
#endif
//------- FIGHTERCRAFT IMPLEMENTATION SECTION --------
#include "Fightercraft.h"
Fightercraft::Fightercraft(): Spacecraft()
{
id = "id default value: Fightercraft";
}
Fightercraft::Fightercraft( char *ptrID ): Spacecraft()
{
id = ptrID;
}
Fightercraft::Fightercraft( char *ptrID, IDirect3DDevice9 **D3DDevice ): Spacecraft()
{
createMesh( &( *D3DDevice ));
ZeroMemory( &materialShip, sizeof( D3DMATERIAL9 ));
materialShip.Diffuse.r = materialShip.Ambient.r = 1.0f;
materialShip.Diffuse.g = materialShip.Ambient.g = 1.0f;
materialShip.Diffuse.b = materialShip.Ambient.b = 1.0f;
materialShip.Specular.r = 0.4f;
materialShip.Specular.g = 0.4f;
materialShip.Specular.b = 0.4f;
materialShip.Power = 8.0f;
}
void Fightercraft::setMaterial( D3DMATERIAL9 mat )
{
ZeroMemory( &materialShip, sizeof( D3DMATERIAL9 ));
materialShip.Diffuse.r = materialShip.Ambient.r = 1.0f;
materialShip.Diffuse.g = materialShip.Ambient.g = 1.0f;
materialShip.Diffuse.b = materialShip.Ambient.b = 1.0f;
materialShip.Specular.r = 0.4f;
materialShip.Specular.g = 0.4f;
materialShip.Specular.b = 0.4f;
materialShip.Power = 8.0f;
}
void Fightercraft::createMesh( IDirect3DDevice9 **D3DDevice )
{
if( FAILED( D3DXCreateBox( *D3DDevice, 2, 2, 2, &meshShip, NULL )))
{
MessageBox( 0, "Aircraft::Aircraft( char *ptrID, Engine *ptrEngine, IDirect3DDevice9 *D3DDevice ) - D3DXCreateSphere - FAILED", 0, MB_OK);
}
}
void Fightercraft::setID( char *ptrID )
{
Spacecraft::setID( ptrID );
}
ID3DXMesh* Fightercraft::getMesh()
{
return meshShip;
}
D3DMATERIAL9 Fightercraft::getMaterial()
{
return materialShip;
}
char* Fightercraft::getID()
{
return Spacecraft::getID();
}
Sorry about the long code sample guys.
The error I am getting is
error C2039: 'getMaterial' : is not a member of 'Vehicle'
I thought my player variable can reference the getMaterial function because it is inherited by all child classes as well.
All the text I've read and all the on-line stuff I research tells me what I am doing should be ok.. yet I don't understand this compile error.
I am missing a fundamental concept with this line I think
Vehicle *player = new Fightercraft( "player", &D3DDevice );
Obviously player variable is still being view as a Vehicle object.
Sorry about the long post guys :(
How can I reference getMaterial function?