Archived

This topic is now archived and is closed to further replies.

(SOLVED) Vector data... disappears?

This topic is 4947 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

There's only 5 classes (even remotely) involved with this: CGame, CPlayer, CUniverse, CPlanet and CShip. CGame holds a pointer to a CPlayer and CUniverse instance. CPlayer holds a pointer to that same CUniverse instance. CUniverse holds a std::vector m_Planet; and CPlanet holds a std::vector m_ShipsForHire; This m_ShipsForHire is where the problem is at. So what happens? The user starts a new game, an instance of CPlayer is created and the pointer m_pPlayer is pointed at it. Next, the m_pPlayer->Init function is called:
void CPlayer::Init(CUniverse* pUniverse){
	m_pUniverse = pUniverse;
	AddShip(0);
	m_PlayerShips.at(0).SetPilot(PILOTS_PLAYER);
	m_pUniverse->GetPlanet(0).Land(pUniverse, GetCurPass(), m_iGoodwill, m_Now);
LogError("CPlayer::Init GetShipsForHireSize() = %u", m_pUniverse->GetPlanet(0).GetShipsForHireSize());
	m_iWage = CalcPlayerExpenses();// personal expenses

}

I Should note, that at this point GetShipsForHireSize() already returns 0, noting the problem has already manifested. (This is also the first call using m_pUniverse->GetPlanet(0). ...)

This in turn calls m_pUniverse->GetPlanet(0).Land(...);
   void CPlanet::Land(CUniverse* pUniverse, int iTotalCurPass, int iGoodwill, CTimeAndDate tad){
//other initialisations also go here... but I can't check those yet

	CShip addship;
	for(DWORD i=0; i<m_RentMaxEachShipType.size(); i++){
		int max = m_RentMaxEachShipType.at(i) - (rand() % (1 + m_RentMaxEachShipType.at(i) / 2)); 
		for(int j=0; j<max; j++){	//create (half of 9, rounded down, +1) to 9 new random ships

			addship.Init(&pUniverse->GetShipT(i), i, false, tad);
			m_ShipsForHire.push_back(addship);
		}
	}
LogError("GetShipsForHireSize() = %u", GetShipsForHireSize());
}
Now this LogError logs (at least it did last time - rand()): GetShipsForHireSize() = 18. And there's nothing between that land call and the CPlayer's LogError... It's almost as though I passed by value instead of passed by reference... but how could a member variable std::vector just vaporise it's contents? [edited by - kVandaele on May 28, 2004 4:01:38 AM]

Share this post


Link to post
Share on other sites
Well, the problem could be in CUniverse::GetPlanet(). If this returns an object instead of a reference to an object, you''re calling Land() on a copy of the planet object, which would explain why the vector is empty on the next line (it''s a different vector since you''re working on a different copy of the CPlanet object!)

Share this post


Link to post
Share on other sites
... amazing how two lines make a miracle (or rather, a curse) vanish

And that''s probably the same problem I''m having with my std::string''s that also seem to have disappeard. Gah, this should be something I remember (instead of wasting 10 hours should''ve just come here! )

Share this post


Link to post
Share on other sites