View Single Post
Old Nov 30th, 2006, 10:45 PM   #28
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
You are right, I was thinking about that too.
[code]
Here's my problem, it doesn't compile:
bool energySourceAt(int r, int c) const
//      If there is an energy source at coordinates (r,c), return true;
//      otherwise, return false.
{
	if (EnergySource(r,c))
		return true;
	else
		return false;
}
You need to take a step back and try to work out what you want the energySourceAt method to do, before throwing code at it.

Write down a list of steps, in English, of how the energySourceAt method could check whether there is an energy source at a particular location. Dont even worry about where the list is, just pretend you have a pile of pieces of paper with an energy location on each one, how would you go about finding whether a given location had an energy source.

The code you have at the moment just creates an EnergySource object inside the test of an if statement.

Quote:
Originally Posted by aznballerlee View Post
if (m_sources[MAXSOURCES] = energySourceAt(m_row, m_col))
For one line, responding to "look through the array" you have a lot of problems:
- I'm not even sure where you were thinking of putting that code. If it is inside the energySourceAt method, then it will be infinitely recursive. If you were going to put it in the Robot step method, then Robot doesn't have an m_sources or and energySourceAt function.
- m_sources[MAXSOURCES] is outside the boundaries of m_sources, which may cause a crash.
- You shouldn't be doing an assignment inside an if test expression
- energySourceAt returns a boolean, you can't compare that with a pointer to an energySource.
- To look through an array you need a loop.
The Dark is offline   Reply With Quote