Advertisement

ADT problems -long

Started by August 15, 2001 04:14 AM
7 comments, last by asaari 23 years, 6 months ago
Im having problems with a few classes of mine they are ADTs and multiply inherited, here is the code:
    
#ifndef _OBJECT_H
#define _OBJECT_H

//Includes

//***************************************************************************
#include <math.h>
#include <gl/gl.h>

#include "vectorlib.h"

//Constants and enumerations
//***************************************************************************

/**************************************************************************
/**************************************************************************
//	CObject: ADT:
//
//	an object has a position, size, angle, and direction
//	but it has no velocity, speed, 
//
***************************************************************************
***************************************************************************/
class CObject
{
	
protected:

	vector3_t position;
	
	/**************************************************************************
	// Update()
	// pure virtual:	***************************************************************************/
	virtual void Update( float deltaTime ) = 0;

public:
	
	float size;	
	float angle;
	float direction;
	
	objectType_t type;
	
	CObject() { type = OBJECT; }        // constructor
	~CObject() { }				       // destructor
	
	/**************************************************************************
	// SetPosition()
	// inline: overloaded: takes 3 floats as parameters
	// sets the model's position in the world
	***************************************************************************/
	int SetPosition( float x, float y, float z )
	{
		position.x = x;
		position.y = y;
		position.z = z;
		return 0;
	}
	
	/**************************************************************************
	// SetPosition()
	// inline: overloaded: takes a vector3_t as a parameter
	// sets the model's position in the world
	***************************************************************************/
	int SetPosition( vector3_t newPos )
	{
		position = newPos;
		return 0;
	}
	
	/**************************************************************************
	// GetPosition()
	// inline: 
	// gets the model's position in the world, returns a vector
	***************************************************************************/
	vector3_t GetPosition()
	{
		return position;
	}

};

void CObject::Update( float deltaTime)
{
	glTranslatef( position.x, position.y, position.z );
}


#endif
  

and then derived from that is:

      
#ifndef _ENTITY_H
#define _ENTITY_H

#include <math.h>

#include "object.h"
#include "vectorlib.h"

/**************************************************************************
/**************************************************************************
//	CEntity: ADT:
//
//  an entity has a maximum speed, a current speed,
//	a velocity and an acceleration
//
***************************************************************************
***************************************************************************/
class CEntity : public CObject
{
	
protected:

	vector3_t velocity;
	vector3_t acceleration;
	
	/**************************************************************************
	// Update()
	// will perform basic calculations on acceleration, velocity, and
	// update the entity's position
	// derived classes should call CEntity::Update, and then do their
	// respective calculations
	***************************************************************************/
	virtual void Update( float deltaTime) = 0;


public:
	
	float maxSpeed;
	float currSpeed;
	
	CEntity()			// constructor
	{ 
		type = ENTITY;
	
		position = 0.0f;
		velocity = 0.0f;
		acceleration = 0.0f;
		
		maxSpeed = 1.0f;
		currSpeed = 0.0f;
		size = 1.0f; 
	}

	~CEntity() { };		// destructor

	/**************************************************************************
	// SetVelocity()
	// inline: overloaded: takes 3 floats as parameters
	// sets the model's velocity in the world
	***************************************************************************/
	int SetVelocity( float x, float y, float z )
	{
		velocity.x = x;
		velocity.y = y;
		velocity.z = z;
		return 0;
	}
	
	/**************************************************************************
	// SetVelocity()
	// inline: overloaded: takes a vector3_t as a parameter
	// sets the model's velocity in the world
	***************************************************************************/
	int SetVelocity( vector3_t newVel )
	{
		velocity = newVel;
		return 0;
	}
	
	/**************************************************************************
	// GetPosition()
	// inline: 
	// gets the model's velocity, returns a vector
	***************************************************************************/
	vector3_t GetVelocity()
	{
		return velocity;
	}

};

void CEntity::Update( float deltaTime)
	{
		velocity += acceleration * deltaTime;
		position += velocity * deltaTime;

		CObject::Update( deltaTime );
		
	}

#endif
  

and then finally if derive an MD2Entity class from an MD2Model calss and Entity,

      
#ifndef _MD2ENTITY_H
#define _MD2ENTITY_H

#include <math.h>

#include "md2.h"
#include "entity.h"
#include "vectorlib.h"


class CMD2Entity : public CMD2Model, public CEntity
{

protected:

public:
	
	CMD2Entity();        // constructor
	~CMD2Entity();       // destructor
	
	/**************************************************************************
	// Update()
	// render model with interpolation 
	***************************************************************************/
	void Update( float deltaTime );
};

#endif
  
and then the cpp file for it:
      
#include "MD2Entity.h"

/*********************************************************************************************
// CMD2Entity::CMD2Entity()
// constructor: sets default value for everything
**********************************************************************************************/
CMD2Entity::CMD2Entity()
{
	position = 0.0f;
	velocity = 0.0f;
	acceleration = 0.0f;
	
	maxSpeed = 10.0f;
	currSpeed = 0.0f;
	size = 24.0f;

	type = ENTITY;
}

/*********************************************************************************************
// CMD2Entity::~CMD2Entity()
// destructor: nothing to do 
**********************************************************************************************/
CMD2Entity::~CMD2Entity()
{
}

/**************************************************************************
// CMD2Entity::Update()
// Update()
// render model with interpolation ***************************************************************************/

void CMD2Entity::Update( float deltaTime )
{
	glPushMatrix();
		CEntity::Update( deltaTime );
		glRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
		glRotatef( 270.0f, 0.0f, 0.0f, 1.0f );
		glRotatef( angle, 0, 0, 1 );
		Animate();
	glPopMatrix();

}
  

sorry for being so long winded!

ok now my problem,

when i compile under VC++6, it compiles without error, but when I go to link i get:

      
Linking...
LINK : warning LNK4075: ignoring /INCREMENTAL due to /FORCE specification
snow.obj : warning LNK4006: "protected: virtual void __thiscall CObject::Update(float)" (?Update@CObject@@MAEXM@Z) already defined in MD2Entity.obj; second definition ignored
snow.obj : warning LNK4006: "protected: virtual void __thiscall CEntity::Update(float)" (?Update@CEntity@@MAEXM@Z) already defined in MD2Entity.obj; second definition ignored
Debug/snowstorm.exe : warning LNK4088: image being generated due to /FORCE option; image may not run
    
I put the /FORCE flag on for the linker, so it builds and runs fine, but if I dont /FORCE then it wont link, and either way i get these errors, ive done everything I can to get rid of them, and the only thing that works is getting rid of the definitons for CObject::Update(); and CWntity::Update();, and I dont want to do that! what I am trying to do is instead of having to rewrite all of the Update() method for every derived class i just put the base class's Update function into the derived classes Update function, so when I go to CMD2Entity::Update(), it will call CEntity::Update(), which in turn will call CObject::Update(), etc. this is why i want to keep the definitions for the Update() methods even though they are pure virtual, sorry for being so long winded and making such a code dump here, (i dont have a website i oculd've put the source code on) thanks in advance [EDIT] some of the C-style comments got messed up sorry aobut that ;( Edited by - asaari on August 15, 2001 5:21:39 AM
please help me out here!
Advertisement
Patience...

If you define a function in a header file, and include that header file more than once (eg. in snow.cpp and MD2Entity.cpp, perhaps?) then it gets compiled more than once, and the linker sees it more than once.

Put function definitions in the cpp files, not the headers. The only good reason to put function definitions in headers is when you want them to be inline, and you can''t make virtual functions inline.
CObject and CEntity only have one function definition a piece so I was trying to save space, and the implementations are within the #ifndef and #endif, ill try putting themn in cpp files though

Edited by - asaari on August 16, 2001 11:49:49 PM
thankyou!!! it worked, but now I have another problem,

in object.h I #include so that i can glTranslatef(), but when i do this, i get some strange errors...
  Compiling...object.cppc:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing '';'' before type ''void''c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: ''WINGDIAPI'' : missing storage-class or type specifiersc:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file foundError executing cl.exe.  


now i know there cant be a problem with my gl.h , and all is good if i take it out of object.h (it is included in other parts of the program as well, any ideas? thanks in advance
Hmm, multiple inheritence is generally Bad with a capital B. If you can rework your design so it is single inheritance (unless all but 1 class are pure virtual abstract classes).
Ohhh, your lucky I''m not in an OO evangilist mood!!

Have a nice day
Brad
Advertisement
the only classes I have that are not ADTs are CMD2Entity, and CMD2Model, both CObject and CEntity are abstract, and to do single inheritance here I think i would have to cut and paste all of the MD2 class into the MD2Entity class, and that seems really messy, and unnecessary
No idea how to fix that, but it looks like WINGDIAPI isn''t defined. Make sure you have all the relevant #includes there. (wingdi.h might be your first call.)
I got it fixed
i dint have some other things included that I shouldve

This topic is closed to new replies.

Advertisement