View Single Post
Old Nov 30th, 2006, 1:45 AM   #13
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
bool Valley::addEnergySource(int r, int c)
{
	//      TODO: implement addEnergySource
	//      If MAXSOURCES have already been added, return false.  Otherwise,
	//      dynamically allocate a new energy source at coordinates (r,c).
	//      Save the pointer to the newly allocated energy source and return true.
	//      (Hint:  The Valley class could contain a private array with MAXSOURCES
	//      elements.)
	if (m_nsources == MAXSOURCES)
		return false;
	else
	{
		EnergySource* es = new EnergySource(r,c);
		return true;
	}
}
Quote:
Originally Posted by aznballerlee View Post
Initially, m_nsources is 0. When we created the new EnergySource, a pointer is created to m_sources[m_nsource]. Once the source has been recorded in the array, we increment m_nsources. Repeatedly, when addEnergySource is called again and again, the m_nsources is incremented, until it exceeds the MAXSOURCES.


This is my problem.
What you need to do is to change the code in red to match the text in red. At the moment you are creating a new EnergySource, but then not doing anything with the resulting pointer.

Quote:
At this point, I'm still confused as to the storing array.
I don't know which is the private array, but I do know that is can hold MAXSOURCES elements.
The private array is m_sources - you already said that in your post above. The size of the m_sources private array in your original code was MAXROBOTS, which is not the same as MAXSOURCES.
The Dark is offline   Reply With Quote