Skip to main content
gamedev.net
🔒 Locked

Nested class problems... [SOLVED]

Started by Rasmadrak Dec 29, 2006 at 2:05 PM 3 replies 700+ views
Original Post
Rasmadrak
Rasmadrak
Hi! I'm having a rather nasty time trying to get my nested classes working together with stl. As soon as I've executed the code below and the function that called the push_back is finished, the program hangs. Is there something wrong with how I'm adding the objects to the lists, etc? If I skip the Model = new model(..) part, things works just fine... why is that?


//Program hangs after calling this function:
void Add(char *file, vec pos)
  {
		 bo test("e:\\tes.lwd");
		 test.pos = pos;
	         Objects.push_back(test);
  }


Baseobject class (bo):



class bo
{
// variables: ------------------------------------------------------------------
public:
//extra stuff removed, since it's not relevant for this issue.
model *Model;

// constructor / destructor:  --------------------------------------------------

 bo(char *file="")
  {
	Model = NULL;

	if (std::strcmp(file,"")!=0)
	{
	Model = new model(file);
	}

  };
};


and finally, the model class:


struct vertex
{
	float x,y,z;
};

struct object
{
list<vertex> Vertex;

};

struct group
{
bool Blended;
vector<int> Texture;
list<object> Objects;

};

class model
{
public:
bool Loaded;
char *name;
list<group> Group;


model(char *file)
	  {
	  Loaded=false;
	  name = file;

		  //try to load "file".
		  // for (each texture)
		   {
		   group thisgroup;
		   int thistexture = SetTexture(x);

		  thisgroup.Texture.push_back(thistexture);

		  for (int a = 0; a < objectcount; a++) {  //nr of objects
			  object thisobject;

			  for (int b = 0; b < vertexcount; b++) {  //nr of vertices
			  vertex V;
			  inFile >> V.x >> V.y >> V.z;
			  thisobject.Vertex.push_back(V);
			  }
			  thisgroup.Objects.push_back(thisobject);
		  }
		  Group.push_back(thisgroup);
		  }
	  Loaded=true;
	  }

};



I would be truly grateful for any help you can provide, since I'm having a hard time visualizing the problem in this case... It could be that the push_back's are performed locally and trown away when the Add-function is ended... but that doesn't explain the freezing of the program, does it? Once again, thanks for your time and help! /Robert [Edited by - Rasmadrak on December 29, 2006 3:05:20 PM]
"Game Maker For Life, probably never professional thou." =)
ToohrVyk
ToohrVyk
You have three push_back calls in there, and the program could hang for any number of reasons (for instance, recursive constructor calls, or infinite loops). I also couldn't find the definition of objectcount.

Step through the program using a debugger, and see why (or if) the push_back call creates problems. This should help you solve the problem, and become more independent.
Rasmadrak
Rasmadrak
I've debugged it step by step, and the error is still nowhere to be found. The creation of the bo's goes without a hitch, but as soon as I leave the Add function, it loops - even to shut down the program takes ages since it's non-responsive. ..

objectcount and vertexcount are variables that gets loaded from "char *file".


// update:


I've just tried removing the model *Model part completely, and placing all the functions from the model inside the bo's class. It worked right away, no problems at all. Weird.

I'll investigate this further, but I really really do believe that the push_back's are performed locally and are "wasted". Weird that I've never got this problem before, since this is a normal way of doing stl stuff for me. :)

Cheers, all!
"Game Maker For Life, probably never professional thou." =)
ToohrVyk
ToohrVyk
Quote:
Original post by Rasmadrak
I've debugged it step by step, and the error is still nowhere to be found. The creation of the bo's goes without a hitch, but as soon as I leave the Add function, it loops - even to shut down the program takes ages since it's non-responsive.


Make sure you "step into" functions when debugging. This should take care of the infinite loop.

Rasmadrak
Rasmadrak
Hi again,

Thanks for your input!
I've managaed to find the problem, which was nested constructors. Dunno if it's a compiler error or whatever, but if I did a separate Load function for the model class, it works fine.

Thanks again!

/Mr Happy Foofoo.
"Game Maker For Life, probably never professional thou." =)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.