Inheritance and Constructor

Hi everybody!

I'm stuck yet again.. This time with inheritance and calling child constructors.

Trying to call a PhysicsObject(int i) constructor from the GameObject class:
1
2
3
4
5
6
7
class GameObject : public PhysicsObject, public CMesh
{
public:
	GameObject();
	GameObject(int i) : PhysicsObject(int i);
	~GameObject();
};


1
2
3
4
5
6
7
class PhysicsObject
{
public:
	PhysicsObject();
	PhysicsObject(int i);
	~PhysicsObject();
};


..when I do that, I get heaps of errors!

Error 1 error C2144: syntax error : 'int' should be preceded by ')' \assignment 3\spaceinvaders\spaceinvaders\gameobject.h 31 SpaceInvaders

Error 2 error C2612: trailing 'type' illegal in base/member initializer list \assignment 3\spaceinvaders\spaceinvaders\gameobject.h 31 SpaceInvaders

Error 3 error C2059: syntax error : ')' \assignment 3\spaceinvaders\spaceinvaders\gameobject.h 31 SpaceInvaders

..and so on - there are 8 errors in total. I don't understand why this is happening, since I have made another sample project to test out inheritance and everything is working fine. Here's that "test" project:
1
2
3
4
5
6
7
8
9
10
class Base
{
private:
	int i;
public:
	Base();
	Base(int newI);
	~Base();
	int getInt();
};

1
2
3
4
5
6
7
8
9
10
11
class Derived : public Base
{
private:
	char c;
public:
	Derived();
	Derived(char newC);
	Derived(char newC, int newI) : Base(int newI);
	~Derived();
	char getChar();
};


Any ideas what might I be doing wrong?
Last edited on
Do you have code for CMesh class?, post here please.
Sure!

This is the complete code for all 3 classes concerned:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#if !defined(CMESH_H)
#define CMESH_H

#if !defined(CTexture_H)
	#include "CTexture.h"
#endif

#if !defined(D3DX9_H)
	#include<d3dx9.h>
#endif

class CMesh
{
private:
	//Object attributes, textures, materials, mesh
	LPD3DXBUFFER		m_pMaterialBuffer;			//Material Buffer for the mesh
	
	// Get a pointer to the buffer that was sent to D3DXLoadMeshFromX().
	D3DXMATERIAL*		m_pMatMaterials;				//not sure what this is

	LPD3DXMESH			m_mesh;						//Custom Mesh
	D3DMATERIAL9*		m_pMaterialList;			//List of materials for the model
	LPDIRECT3DTEXTURE9* m_pTextureList;				//List of textures for the model - replaced by CTexture
	//CTexture* m_pTextureList;						//List of textures for the model
	DWORD				m_nNumMaterials;			//Number of mesh materials
public:
	//constructors and destructor
	CMesh();
	~CMesh();
	

	//loads a mesh an returns true if successful
	//not a pointer to pD3D_Device?? why???
	bool			loadMesh		(LPDIRECT3DDEVICE9 pD3D_Device, char fileName[]);

	//returns the number of materials associated with the mesh
	DWORD			getNumMaterials	();

	//returns a pointer to the material for the mesh. The material number is specified by the parameter
	D3DMATERIAL9*	getMeshMaterial	(int i);

	//returns a pointer to the texture for the mesh. The texture number is specified by the parameter
	LPDIRECT3DTEXTURE9*		getMeshTexture	(int i);

	//Renders the mesh on screen
	//again why not a pointer?
	void renderMesh(LPDIRECT3DDEVICE9 pD3D_Device);

};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#if !defined(PHYSICSOBJECT_H)
#define PHYSICSOBJECT_H

#if !defined(D3DX9_H)
	#include<d3dx9.h>
#endif

class PhysicsObject
{
private:
	//The Main Matrix
	D3DXMATRIX* WorldSpace;
	
	//Support Matrix
	D3DXMATRIX* m1;

	//Scale
	float m_fScaleX;
	float m_fScaleY;
	float m_fScaleZ;

	//Rotation
	float m_fRotationX;
	float m_fRotationY;
	float m_fRotationZ;

	//Translation
	float m_fTranslationX;
	float m_fTranslationY;
	float m_fTranslationZ;

	//Velocity
	float m_fVelocityX;
	float m_fVelocityY;

	//Radius used for sphere collision
	float m_fRadius;

public:
	//constructors and destructor
	PhysicsObject();
	PhysicsObject(int i);
	PhysicsObject(float translateX, float translateY, float translateZ);
	~PhysicsObject();

	//these 3 methods change all 9 attributes for the PhysicsObject
	void scale		(float x, float y, float z);
	void rotate		(float x, float y, float z);
	void translate	(float x, float y, float z);
	
	//setVelocity
	void setVelocity(float x, float y);

	//returns a transformation matrix based on the object attributes
	D3DXMATRIX* transform();

	//updates the object position? Not sure yet...
	void update(float timeStep);
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#if !defined(GAMEOBJECT_H)
#define GAMEOBJECT_H

#if !defined(PHYSICSOBJECT_H)
	#include "PhysicsObject.h"
#endif

#if !defined(CMESH_H)
	#include "CMesh.h"
#endif

#if !defined(D3DX9_H)
	#include<d3dx9.h>
#endif

class GameObject : public PhysicsObject, public CMesh
{
private:
	
public:
	GameObject();
	GameObject(int i) : PhysicsObject(int i);
	~GameObject();
};

#endif 


am not so expert but i think this will help

What is inherited from the base class?

In principle, a derived class inherits every member of a base class except:

>its constructor and its destructor
>its operator=() members
>its friends
or you should overload the constractor,(below).
you may help me either, what you mean by

GameObject(int i) : PhysicsObject(int i);
Last edited on
I think you should write:
GameObject(int i) : PhysicsObject(i);

instead of
GameObject(int i) : PhysicsObject(int i);
Hmm... I'm not really sure..
GameObject(int i) : PhysicsObject(i); doesn't work sadly..

I am referring to another 'test' project that I put together to test inheritance and this type of stuff works without a problem...
GameObject(int i) : PhysicsObject(int i)
Last edited on
I can't see that if your test project looked like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Base
{
private:
	int i;
public:
	Base();
	Base(int newI);
	~Base();
	int getInt();
};

class Derived : public Base
{
private:
	char c;
public:
	Derived();
	Derived(char newC);
	Derived(char newC, int newI) : Base(int newI); //error here
	~Derived();
	char getChar();
};

How that would work either...
I think you are mistaken
1
2
3
GameObject( int i ) :PhysicsObject( i ) 
{
}


If inside the class declaration otherwise

1
2
3
GameObject::GameObject( int i ) :PhysicsObject( i ) 
{
}

Topic archived. No new replies allowed.