![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
Quote:
|
|
|
|
|
|
|
#12 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#13 | ||
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
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:
Quote:
|
||
|
|
|
|
|
#14 | ||
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
Quote:
EnergySource* m_sources[m_nsource] = new EnergySource(r,c); m_nsource++ Quote:
EnergySource* m_sources[MAXSOURCES]; |
||
|
|
|
|
|
#15 | ||
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
Quote:
Quote:
From your original code in the Valley class: private: Robot* m_robots[MAXROBOTS]; int m_nrobots; EnergySource* m_sources[MAXROBOTS]; int m_nsources; }; |
||
|
|
|
|
|
#16 | |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
I don't understand your suggestion:
Quote:
private: Robot* m_robots[MAXROBOTS]; int m_nrobots; EnergySource* m_sources[MAXSOURCES]; int m_nsources; |
|
|
|
|
|
|
#17 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
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". |
|
|
|
|
|
#18 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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 . |
|
|
|
|
|
#19 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#20 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 2
![]() |
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |