Sign in to follow this  
Rasmadrak

Using STL list inside another class?

Recommended Posts

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? :/

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ... :/

Share this post


Link to post
Share on other sites
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 this

class 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,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this