Archived

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

Brin

Self Referenced Linked List Classes

Recommended Posts

Whew, say that 10 times. Anyways... Alright, I have a class called BULLET. Inside I have: BULLET *next, *newbullet; Now, I want to create a linked list with this all by itself. So in a class called Player I have: BULLET *bullet; To shoot the bullet I call: Player->ShootBullet(BULLET *bullet, Uint32 x, Uint32 y); This is ShootBullet: void BULLET::ShootBullet(BULLET *bullet, Uint32 x, Uint32 y) { newbullet = bullet; if(!bullet) { buglog.ReallyWrite("Tested NULL... It''s NULL"); bullet = new BULLET(x, y); bullet->next = newbullet; newbullet->next = NULL; } else { buglog.ReallyWrite("Tested NULL... Not NULL"); newbullet = new BULLET(x, y); newbullet->next = bullet; bullet = newbullet; newbullet->next = NULL; } } Btw, when I say new BULLET(x, y); Im just filling in the info inside of newbullet. IE: (newbullet->rBullet.x = x Alright. Here is my problem, Whenever I go to push the shootbullet button it just crashes.. Stackdump. WHY?! Also, I know there are other ways of doing this, but THIS is what I want... So don''t try to change my mind... at all.

Share this post


Link to post
Share on other sites
Why is it a member function if it''s not operating on an instance of Bullet? Is it static? Why does player have a pointer to a single bullet?

Anyway, here''s your problem (well, apart from bizarre coding conventions):

newbullet = bullet;

if(!bullet) { // This line means newbullet = NULL too
buglog.ReallyWrite("Tested NULL... It''s NULL");
bullet = new BULLET(x, y);

bullet->next = newbullet;
newbullet->next = NULL; // newbullet is still NULL, but you''re dereferencing it
}


Also, this last bit makes no sense:

newbullet->next = bullet; // Here, you set newbullet''s next pointer to be the same as bullet
bullet = newbullet;
newbullet->next = NULL; // And here, you set it right back to NULL again.


Personally, it looks like you don''t quite understand pointers properly. Ask for clarification if these points don''t show you exactly what you''re doing wrong.

Share this post


Link to post
Share on other sites