Hello,
I am currently trying to write a board game, and I am having trouble making each GameTile on the board access the same Point on the board.
Each GameTile contains a certain number of Points, and sometimes Points overlap(the Point on a corner of one tile may be the same Point on the corner of an adjacent tile)
Each Point on the board has a boolean value to determine whether that Point is occupied or not.

In the code below, I have two GameTiles, tile1 and tile2 respectively.

#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 point1;
    Point point2;

    tile1.addElement(0,point1);
    tile1.addElement(1,point2); // tile contains 2 points

    tile2.addElement(0,point1);
    tile2.addElement(1,point2); // this tile contains 2 points(which are the same points as the above tile)

    bool yes = true;
    bool no = false;

    tile1.setSurroundingPointOccupied(0,yes);
    tile1.setSurroundingPointOccupied(1,no);

    cout << tile1.checkSurroundingPointOccupied(0) << endl;
    cout << tile1.checkSurroundingPointOccupied(1) << endl;

    cout << tile2.checkSurroundingPointOccupied(0) << endl;
    cout << tile2.checkSurroundingPointOccupied(1) << endl;

    return 0;
}

I have attempted to make each GameTile contain 2 Points(which are the same Points, for testing purposes) and when I edit the boolean value for point1 through tile1, I expect this value to have be changed within tile2 as well. (For example, when I set point1's boolean to true, I expect that when I return the value of point1 from within tile2, it will return 1, without directly setting the variable within tile2.)

The output I expected:
1
0
1
0

But the actual output on running the program:
1
0
0
89 (or another large number, changes each time program is run)

I believe Pointers are the way to go with this, however I am new to them, and despite reading various tutorials on Pointers, I'm still obviously doing something wrong.
Any help on how to make this program do what I want it to will be greatly appreciated.

By the way, I am running this using Code::Blocks 8.02 and the GNU GCC Compiler if this is of any help.

Salem commented: Nice first post - code tags, description and question - all without help form the mods :) +20

> pSinglePoint = &next;
Two things wrong here.
1. You're attempting to point from inside the class, to some outside the class. A class object should be a self-contained entity. Having invisible dependencies to the outside world is a recipe for disaster.
2. You're actually storing a pointer to the local parameter, NOT the original in main.

Thank you for your reply, I thought my code did something else than what you have taught me so I will structure my program a different way. Thanks again! :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.