Archived

This topic is now archived and is closed to further replies.

Please help with void*

This topic is 5589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello- I am trying to create a manager which looks after a string of projectile classes. I have a bunch of different classes that it can create, so i want to use a void* to reference the object i create on the heap.. here is the error code: ''type cast'' : cannot convert from ''void *'' to ''class CRifleAmmo'' No constructor could take the source type, or constructor overload resolution was ambiguous here is the source that is causeing me problems:
  
(CRifleAmmo)pProjectile = new CRifleAmmo;
				this->AddRef();
				pProjectile->SetTexture(RifleProjectileTexture);
				return;
  
here is the class:
  
class CRifleAmmo:public CProjectile
{

public:

	CRifleAmmo();
	~CRifleAmmo(){};

private:

	

};

CRifleAmmo::CRifleAmmo()
{
	//Speed = 10

	//Damage level = 30%



};
  
i can show you the base class if you like. I know i need to typecast the void* into the appropriate type, but it won''t let me do it. What am i doing wrong?

Share this post


Link to post
Share on other sites
My understanding is that void pointers are shunned in C++ programming and that the proper way to approach the issue is to use the base class from which the target class is derived - that would be your projectile class. If SetTexture is a method of projectile class, and CRifleAmmo is derived from that, then you shouldn''t have to do any casting to call that method.

CRifleAmmo pProjectile = new CRifleAmmo;
pProjectile->SetTexture(RifleProjectileTexture);

At any rate, it''s not clear at all what your trying to do and that error suggests to me that you might want to reconfigure your approach all together.

Share this post


Link to post
Share on other sites
What i was hoping to accomplish is a general method in the Manager which gets called every time a request for a projectile is made. it then creates a new projectile on the heap of the type needed (in this case a rifle ammo shot). here is the base class code:


  
class CProjectile
{

public:

CProjectile();
~CProjectile();

virtual void Update();
virtual CProjectile *GetNextProjectile();
virtual void SetNextProjectile(CProjectile* pNextOne);
virtual void SetTexture(LPDIRECT3DTEXTURE8 Texture);

private:

LPDIRECT3DTEXTURE8 ProjectileTexture;

D3DXVECTOR2 Translation;
D3DXVECTOR2 RotationCenter;
D3DXVECTOR2 Scale;

D3DXVECTOR2 Velocity;
D3DXVECTOR2 Position;
D3DXVECTOR2 StartingPos;

CProjectile *pNextProjectile;

bool bAlive;

};

CProjectile::CProjectile()
{
ProjectileTexture = 0;

Translation.x = 0.0f;
Translation.y = 0.0f;

RotationCenter.x = 0.0f;
RotationCenter.y = 0.0f;

Scale.x = 1.0f;
Scale.y = 1.0f;

bAlive = false;

Velocity.x = 0.0f;
Velocity.y = 0.0f;

Position.x = 0.0f;
Position.y = 0.0f;

StartingPos.x = 0;
StartingPos.y = 0;

ProjectileTexture = 0;

}


this is kinda why a void pointer would work i think... the types change constantly, and i don''t want to create a pointer for each type of projectile texture, (unless i have to).

oh... you know what i could do is create a CProjectile* and then cast that as needed. i''m not sure that will work.. i will try it now.

but that is the general procedure... and what i am hoping to achieve.

Thanks for your help again, BTW..


Share this post


Link to post
Share on other sites
I''m guessing that you can''t cast from void* to CRifleAmmo because the latter isn''t a pointer. (Void pointers can point to anything, but they''re still pointers, not objects.) I would have thought it should be done as pProjectile = (void*) new CRifleAmmo ... but I''m not sure, so don''t just take my word for it.

Share this post


Link to post
Share on other sites
Thanks for your help, but i can''t belive how stupid i am. it was really simple:

CProjectile* pProjectile;
pProjectile = new CRifleAmmo;

LessBread was correct, although i didn''t understand what he was saying..

Yes, that makes sense, because the "new" keyword always returns an address, and not an object per se. damn it... haha... i new that..

Thanks for your help! Everybody have a Guiness!

Share this post


Link to post
Share on other sites
quote:
Original post by MrBeaner
CProjectile* pProjectile;
pProjectile = new CRifleAmmo;

Make sure CProjectile has a virtual destructor, else attempts to "delete pProjectile" will cause undefined behaviour.

Share this post


Link to post
Share on other sites
OK.. thanks... but right now i have to figure out why my header files no longer show up in the project! they disappeared last night.. and i have no idea why...

Share this post


Link to post
Share on other sites