Jump to content
  • Advertisement
Sign in to follow this  
Rasmadrak

Using STL list inside another class?

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
class chunk
{
public:
list<Basic3dObject> Children;

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


That code cannot compile. Were you thinking of something else?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!