Pointers, Classes And Linked Lists [closed]

23 hours ago 1
ARTICLE AD BOX

The game I'm making using C++ requires a linked list like structure to make worms, but I'm unable to figure out how to declare and use nextnode within my worm object.

Here's my code of whatever I tried so far:

#ifndef WORM_H #define WORM_H #include<SFML/Graphics.hpp> #include "Enemy.h" using namespace sf; class Worm: public Enemy { public: Worm(); virtual ~Worm(); void initialise(Vector2f posi,int ind); bool update(Vector2f plocation); protected: private: Worm* nextbody=new Worm; int index=0; }; #endif // WORM_H #include<SFML/Graphics.hpp> #include<random> #include "GlobalValues.h" #include "Worm.h" using namespace sf; Worm::Worm() { //ctor } Worm::~Worm() { delete nextbody; } void Worm::initialise(Vector2f posi,int ind) { //Worm temp(); //nextbody=&temp; nextbody->initialise({posi.x,posi.y+30},ind+1); index=ind; body.setFillColor(Color(255,0,0)); body.setSize({30,30}); body.setOrigin({15,15}); location=posi; body.setPosition(Vector2f((dimensions.x/2)+(location.x-cameralocation.x),(dimensions.y/2)+(location.y-cameralocation.y))); printf("\nHello!"); printf("\n%d %f %f",index,location.x,location.y); if(ind<9) nextbody->initialise({posi.x,posi.y+30},ind+1); else nextbody=NULL; printf("\n%d %f %f",index,location.x,location.y); } bool Worm::update(Vector2f plocation) { body.setPosition(Vector2f((dimensions.x/2)+(location.x-cameralocation.x),(dimensions.y/2)+(location.y-cameralocation.y))); if(harm(plocation)) return true; else if(nextbody!=NULL) nextbody->update(plocation); return false; }

One thing I've noticed is that it exits the SFML window as soon as I try nextbody->initialise({posi.x,posi.y+30},ind+1) as confirmed by the printf statements. and the console says:

Process returned -1073741571 (0xC00000FD) execution time : 4.031 s Press any key to continue.
Read Entire Article