Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2006, 12:52 AM   #11
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Quote:


Does this look right to you? Why should your array for storing energy sources be limited to the maximum number of robots?
MAXSOURCES.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 1:07 AM   #12
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
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.
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 1:45 AM   #13
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
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 online now   Reply With Quote
Old Nov 30th, 2006, 3:09 PM   #14
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Quote:
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.
EnergySource* m_sources[m_nsource] = new EnergySource(r,c);
m_nsource++


Quote:
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.
So would I need to add
EnergySource* m_sources[MAXSOURCES];
as a private array?
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 5:59 PM   #15
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by aznballerlee View Post
EnergySource* m_sources[m_nsource] = new EnergySource(r,c);
m_nsource++
Almost - you don't need to redeclare m_sources as it is already in your Valley class. Delete the code in red.

Quote:
Originally Posted by aznballerlee View Post
So would I need to add
EnergySource* m_sources[MAXSOURCES];
as a private array?
Umm, you already have done that in your Valley class. I assumed you knew that as you said you had modified the code to add it:
From your original code in the Valley class:
private:
	Robot*        m_robots[MAXROBOTS];
	int           m_nrobots;
	EnergySource* m_sources[MAXROBOTS];
	int           m_nsources;
};
The Dark is online now   Reply With Quote
Old Nov 30th, 2006, 6:35 PM   #16
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
I don't understand your suggestion:
Quote:
Umm, you already have done that in your Valley class. I assumed you knew that as you said you had modified the code to add it:
From your original code in the Valley class:
Code:

private: Robot* m_robots[MAXROBOTS]; int m_nrobots; EnergySource* m_sources[MAXROBOTS]; int m_nsources; };
So do I alter the one I add since it was wrong?

private:
	Robot*        m_robots[MAXROBOTS];
	int           m_nrobots;
	EnergySource* m_sources[MAXSOURCES];
	int           m_nsources;
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 6:46 PM   #17
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
Yes you altered the right one. The reason I did not know that you changed the right one is that I can't see your computer. I can only go by the code that you have posted.

My only suggestion was to remove the red bit of code in my last post. At the moment that code is declaring a new local array and then trying to assign a single value to it. It won't even compile. If you remove the red code that is declaring it (the "EnergySource*"), then the compiler will know to use the m_sources member variable from the class. This is what your teacher called a "private array".
The Dark is online now   Reply With Quote
Old Nov 30th, 2006, 6:58 PM   #18
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Okay, I get that. My other problem is implementing this with Robots. I use the same logic:
bool Valley::addRobot(string name, vp, int r, int c, Dir dir)
{
	if (m_nrobots == MAXROBOTS)
		return false;
	else
	{
		m_robots[m_nrobots] = new Robot(name,    ,r, c, dir);
		return true;
	}
}

The compiler says vp is undeclared, which makes sense since I haven't done that other than in the constructor. How do I do this? I thought about using another accessor function or so to declare it .. and someone said to pass it through something whivch I have no clue how to do .
aznballerlee is offline   Reply With Quote
Old Nov 30th, 2006, 7:06 PM   #19
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
You don't need to pass vp into the addRobot function. The valley pointer can be found using the this keyword as you are in a Valley member function.
m_robots[m_nrobots] = new Robot(name, this, r, c, dir);

Also don't forget that you need to increment m_nrobots when you add a robot.
The Dark is online now   Reply With Quote
Old Nov 30th, 2006, 7:16 PM   #20
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 2 aznballerlee is on a distinguished road
Alright, I get that.. when I'm done adding the stuff, I'll have to call the destructor to delete the new array.

Would this be appropriate?
//      TODO:  Implement the destructor
//      Delete any dynamically allocated objects held by the Valley.

Valley::~Valley()
{
	 delete [] m_robots ;
	 delete [] m_sources;
	
}
aznballerlee is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wierd compile Error. Need help please. Keiyentai Java 7 Aug 19th, 2006 1:35 AM
What is: "Oriented programming (OO)?" BrinyCode C++ 12 Nov 22nd, 2005 7:40 AM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
User Input for Number Format ericelysia1 Java 0 Jul 21st, 2005 3:41 PM
Programmers Needed! Online Game troy_eisert C++ 2 Jan 29th, 2005 12:51 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC