Thanks for your reply!
I am trying to add an array of Points to GameTile, tile1 here, but I want to add these exact Points to another GameTile, tile2. When I add an element(tester1) to both tile1 and tile2, I want to be able to edit the boolean information for tester1 from within tile1, and then afterwards, check this boolean information from tile2.
With regard to what you wrote back, I have rewritten the code as follows( I think leaving dynamic memory alone is the best way to go...)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
using namespace std;
class Point
{
private:
bool occupied;
public:
void setOccupied(bool yesOrNo)
{
occupied = yesOrNo;
}
bool checkOccupied()
{
return (occupied);
}
};
class GameTile
{
private:
Point allPoints[2];
Point *pSinglePoint;
public:
void addElement(int arrIndex,Point next)
{
pSinglePoint = &next; // Pointer points to the memory location of "next" element(in this case a "Point")
allPoints[arrIndex] = *pSinglePoint; // element of allPoints = what is located at the memory address of pSinglePoint
}
void setSurroundingPointOccupied(int arrIndex, bool yesOrNo)
{
allPoints[arrIndex].setOccupied(yesOrNo); // This should set the Point boolean value
// to true or false(not a copy, but the actual object is being edited here)
}
bool checkSurroundingPointOccupied(int arrIndex)
{
return(allPoints[arrIndex].checkOccupied()); // This should return 0 or 1.
}
};
int main()
{
GameTile tile1;
GameTile tile2;
Point tester1;
Point tester2;
tile1.addElement(0,tester1);
tile1.addElement(1,tester2); // tile contains 2 points
tile2.addElement(0,tester1);
tile2.addElement(1,tester2); // this tile contains 2 points(which are the same points as the above tile)
bool yes = true;
bool no = false;
tile1.setSurroundingPointOccupied(0,yes); // Changed this from no to yes
tile1.setSurroundingPointOccupied(1,no); // Changed this from yes to no
cout << tile1.checkSurroundingPointOccupied(0) << endl;
cout << tile1.checkSurroundingPointOccupied(1) << endl;
cout << tile2.checkSurroundingPointOccupied(0) << endl;
cout << tile2.checkSurroundingPointOccupied(1) << endl;
return 0;
}
|
:
I think this is closer to the answer I am looking for, the output I expect this time:
1
0
1
0
But the output this time is:
1
0
0
85 (Or some other large number, this one changes each time the program is run)
As can be seen, the program still does not work right. I want to edit tester1 and tester2 from within tile1, and then I want to be able to return these edited boolean values from tile2.
I'm still doing something wrong, but I can't see what the problem might be.
Again, any help would be appreciated.