2 Replies - 821 Views - Last Post: 12 April 2012 - 07:23 AM Rate Topic: -----

#1 Darthlemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-July 11

Vector variables being lost after it's loaded in.

Posted 11 April 2012 - 02:34 PM

I am trying to write just a basic engine that reads in a xml file fills out a vector<struct*>. Everything saves/loads in fine but when i try to copy over the info in main i get garbage values. Here is some snipets of code

bool CLevelReader::LevelParser()
{
	TiXmlDocument doc;

	//attempt to load doc
	if(!doc.LoadFile("Pegs.xml"))
		{
			return false;
		}
					


	// Access the root element ("peg_list"_ in the doc
	TiXmlElement* pRoot = doc.RootElement();
	if(pRoot == nullptr)
		return false;


	// Load each peg data from the document
	// and store the vector
	m_vLoadedLevel.clear();

	TiXmlElement* pPeg  = pRoot->FirstChildElement("peg_info");

        // while there is still info in the xml doc
	while(pPeg != nullptr)
	{
		// Read the peg info from this node
		Peg info = { };


		//Read the type
		double buffer;
		if(pPeg->Attribute("type", &buffer) != nullptr)
		{
			info.m_nType = (int)buffer;
		}

		if(pPeg->Attribute("width", &buffer) != nullptr)
		{
			info.m_nWidth = (int)buffer;
		}

		if(pPeg->Attribute("height", &buffer) != nullptr)
		{
			info.m_nHeight = (int)buffer;
		}

		if(pPeg->Attribute("x", &buffer) != nullptr)
		{
			info.m_fX = (float)buffer;
		}

		if(pPeg->Attribute("y", &buffer) != nullptr)
		{
			info.m_fY = (float)buffer;
		}

		if(pPeg->Attribute("velX", &buffer) != nullptr)
		{
			info.m_fVelocityX = (float)buffer;
		}

		if(pPeg->Attribute("velY", &buffer) != nullptr)
		{
			info.m_fVelocityY = (float)buffer;
		}

		if(pPeg->Attribute("imageID", &buffer) != nullptr)
		{
			info.m_nImageID = (int)buffer;
		}

		// store the peg info
		m_vLoadedLevel.push_back(&info);


		//move to the next pegin the list
		pPeg = pPeg->NextSiblingElement("peg_info");
	}

	return true;
}




The above code works fine and everything is filled out at this point.
Now in main...
	CLevelReader* level = new CLevelReader();
		level->SaveLevel();
		level->LevelParser();
		vector<Peg*> temp;
		//memcpy(temp[0], level->GetLevel()[0], sizeof(Peg));
		for(int i = 0; i < level->GetVector().size(); ++i)
		{
			temp[i] = level->GetVector()[i];
		}




By the time i reach the loop GetVector[i] is garbage values...
Below are some more code snipets
        // in CLevelReader.h
	std::vector<Peg*> GetVector(void){return m_vLoadedLevel;}


struct Peg
{
	int m_nType;
	int m_nWidth;
	int m_nHeight;
	float m_fX;
	float m_fY;
	float m_fVelocityX;
	float m_fVelocityY;
	int m_nImageID;
};




I have tried almost everything i could think of, but i don't really know if i am using pointers correctly, how to really fix the problem. Hence where you guys come in!

Is This A Good Question/Topic? 0
  • +

Replies To: Vector variables being lost after it's loaded in.

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10664
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: Vector variables being lost after it's loaded in.

Posted 12 April 2012 - 06:59 AM

Moved to C/C++. Please don't post programming questions in the Student Campus forum.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4141
  • View blog
  • Posts: 12,892
  • Joined: 25-December 09

Re: Vector variables being lost after it's loaded in.

Posted 12 April 2012 - 07:23 AM

Is there any particular reason you are using a vector of Peg pointers instead of a vector of Peg? Why all the vector<ANYTHING *>? Normally you use vectors to get away from having to dynamically allocate memory.

Jim

This post has been edited by jimblumberg: 12 April 2012 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1