Archived

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

ktuluorion

Any ideas on where the coding mistake is here?

Recommended Posts

Here is a projectile class I am creating for my game. For some reason, upon exiting my game, there is a lockup in the program. My main gameclass.h instantiates a projectileclass. If anyone can spot a stupid error, please let me know here is the projectileclass.
  

struct projectilestruct
{
    projectilestruct *next;
    projectilestruct *prev;
	float x,y,z;
    float distance;
	float xvelocity;
    float yvelocity;
	float zvelocity;
	int model;
};


class projectileclass
{
	projectilestruct *head;
    projectilestruct *temp;
	void clear()
	{
    temp=head;
	while (temp->next!=NULL)
	{
		temp=temp->next;
    if (temp->prev!=NULL)
		delete temp->prev;
	}

	
	if (temp!=NULL)
	delete temp;
	if (head!=NULL)
	delete head;
	
}
	void add(float x, float y, float z, float xvelocity, float yvelocity, float zvelocity, int model)
	{
	temp=head;
	while (temp->next!=NULL)
		temp=temp->next;
    temp->next=new projectilestruct;
	temp->next->prev=temp;
	temp=temp->next;
	temp->x=x;
	temp->y=y;
	temp->z=z;
	temp->xvelocity = xvelocity;
	temp->yvelocity=yvelocity;
	temp->zvelocity =zvelocity;
	temp->model=model;
	temp->distance=0;
	}
    
	void update()
	{
    temp=head;
	while (temp->next!=NULL);
	{
	temp->x+=temp->xvelocity;
	temp->y+=temp->yvelocity;
	temp->z+=temp->zvelocity;
	temp->distance+=temp->xvelocity+temp->yvelocity+temp->zvelocity;
	}
	}
public:
	projectileclass()
	{
    head=new projectilestruct;
	}
    ~projectileclass()
	{
     clear();
	}
    
};


  

Share this post


Link to post
Share on other sites
I see one problem in your clear function. You go to next to delete previous. That means you have to move one object past your list, which should be a NULL value for next, which of course has no previous, unless you''ve implemented it some other way. (I only looked in your destructor function and saw that you called clear and figured the problem was there). Try that and let me know.

Share this post


Link to post
Share on other sites
quote:

If anyone can spot a stupid error, please let me know



Found four
- C++ already comes with a list class.
- you have a pointer to the head of the list ... why do you insist in adding your new element at the end
- Your computation of distance is incorrect.
- You have a destructor, but no copy constructor nor assignment operator.


  
#include <list>
#include <algorithm>

struct ProjectileStruct
{
float x,y,z;
float vx,vy,vz;
float distance;
int model;

ProjectileStruct( float x_, float y_, float z_,
float vx_, float vy_, float vz_,
int model_ )
: x( x_ ), y( y_ ), z( z_ ),
vx( vx_ ), vy( vy_ ), vz( vz_ ),
distance( 0 ), model( model_ )
{
// Yes, you still need the empty body

}

// It''s a static function because I don''t want

// to deal with pointer-to-members and the

// std::mem_fun_ref adapter... would be easy to

// change though.

static void update( ProjectileStruct& proj )
{
proj.x += proj.vx;
proj.y += proj.vy;
proj.z += proj.vz;

// Ideally, you should store distance_squared

// to save the square root, or compute the

// velocity when initially creating the instance.


float velocity = sqrtf( proj.vx * proj.vx +
proj.vy * proj.vy +
proj.vz * proj.vz );

proj.distance += velocity;

// The update should also take into account the

// elapsed time since the last update, but here,

// it would either involve static variables and

// thus be non-reentrant, or involve function

// objects (functors). Since I already didn''t

// want to deal with mem_fun_ref, I am not going

// to head that way either.


}
};

// If you *really* want a wrapper.

class ProjectileClass
{
std::list<ProjectileStruct> projectiles;

public:
void add( float x_, float y_, float z_,
float vx_, float vy_, float vz_,
int model_ )
{
// for compatibility with your code.

add( ProjectileStruct( x_, y_, z_, vx_, vy_, vz_, model_ ) );
}

void add( const ProjectileStruct& proj )
{
// Add a projectile at the end of the list.

projectiles.push_back( proj );
}

void update()
{
// Call ProjectileStruct::update_projectile,

// passing each projectile in the list in turn.


std::for_each( projectiles.begin(), projectiles.end(),
&ProjectileStruct::update_projectile );
}

// Since there are no naked pointers involved, and

// the list elements aren''t pointers either, there

// is no need for a destructor, nor for a clear() method

};


That should work... unless I made stupid errors, of course.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites