Using STL list inside another class?

Started by
3 comments, last by Emmanuel Deloget 18 years, 1 month ago
Hello people! I have a question regarding using "stl list" inside an dynamically created array... i.e if I do:


list<Basic3dObject> Objects;  


I can push and pop objects just fine.... but if I do...

class chunk
{
public:
list<Basic3dObject> Children;

chunk() { 
        Children = new list<Basic3dObject>;
        };
};


...pushing gives an write error? Do I do something really wrong or what? :/
"Game Maker For Life, probably never professional thou." =)
Advertisement
class chunk{public:list<Basic3dObject> Children;chunk() {         Children = new list<Basic3dObject>;        };};


That code cannot compile. Were you thinking of something else?
More specifically it cannot compile because the list does not need to be allocated with new. The constructor is unnecessary, and its contents are incorrect.

Post the real code.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
oops, forgot the "*" :D

class terrainchunk{public:int List;float Radius;     //denna räknas ut beroende på högsta och lägsta punkten osv. minst 5.6*TerrainScale dock.vector3d CenterPos;list<Basic3dObject> *Children;terrainchunk()        {        List=Radius=0;        CenterPos.Set(0,0,0);        Children = new list<Basic3dObject>;        };};





Btw, what I want to do is add an object from the "Object"-list into the "Children"-list ... :/
"Game Maker For Life, probably never professional thou." =)
Actually, using list in this way is not a very good idea. List objcts are not supposed to be created using new (and destroyed using delete) - or course they can, but if you just add an instance of the list to your class then all the creation/destruction of the list object will be automatic (far superio method, because you can't forget something which is automaticly done for you). Why don't you write
#include <list> // don't forget thisclass terrainchunk{public: // ***** ICKS! public member!?!?!?  int List;  float Radius;     //denna räknas ut beroende på högsta och lägsta punkten osv. minst 5.6*TerrainScale dock.vector3d CenterPos;  list<Basic3dObject> Children; // no pointer  terrainchunk()        {        List=Radius=0;        CenterPos.Set(0,0,0);        // Children = new list<Basic3dObject>; // don't need that anymore        };};


Regards,

This topic is closed to new replies.

Advertisement