Problem making two objects access the same data

Hello,

I am trying to write a board game, and I am having trouble making two GameTiles access the same Point on the board. I have to set each point with a boolean value, to check whether it is occupied or not.

Both GameTiles contain the same two Points, as this is for testing purposes at the moment. The idea is when I set the boolean values of the Points contained within tile1 to "no" and "yes" respectively, when I try to return these values from tile2, the values returned should be "no" and "yes" as well.

Here is my simplified code, which is hopefully understandable:
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
#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];

public:

    void addElement(int arrIndex,Point next)
    {
        allPoints[arrIndex] = new Point;
        allPoints[arrIndex] = &next;
    }
    void setSurroundingPointOccupied(int arrIndex, bool yesOrNo)
    {
        allPoints[arrIndex] = new Point;
        allPoints[arrIndex]->setOccupied(yesOrNo);
    }

    bool checkSurroundingPointOccupied(int arrIndex)
    {
        return(allPoints[arrIndex]->checkOccupied());
    }


};

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,no);
    tile1.setSurroundingPointOccupied(1,yes);

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

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

    return 0;
}


Now, this compiles just fine, and the output I expect, should be:

0
1
0
1

However, when I run the program, this is the output I get:

0
1
1
1

I would appreciate any help on how to get this functioning correctly, as even after looking at various Pointer tutorials, I'm still a little unsure as to the ins and outs of them at the moment.

By the way, I am running this using Code::Blocks 8.02 and the GNU GCC Compiler if this is of any help.
Last edited on
Hello to you too. What exactly are you trying to do here? This causes a memory leak:
1
2
3
4
5
void addElement(int arrIndex,Point next)
{
     allPoints[arrIndex] = new Point; //you assign a new Point to allPoints[arrIndex]
     allPoints[arrIndex] = &next; //then you assign it the address of next... new Point is lost...
}

Also, why do you want to mess with dynamically allocated memory? Wouldn't it be simpler if you declared the data in your GameTile class like Point allPoints[2];?
Last edited on
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.
Last edited on
Sorry for the late reply... I hope you're still there...

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
#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]; //<- make this an array of pointers
    //Point *pSinglePoint; <- you don't need this

public:

    //get a Point pointer as a parameter
    void addElement(int arrIndex,Point * next)
    {
        allPoints[arrIndex] = next;
    }

    void setSurroundingPointOccupied(int arrIndex, bool yesOrNo)
    {
        allPoints[arrIndex]->setOccupied(yesOrNo);
    }

    bool checkSurroundingPointOccupied(int arrIndex)
    {
        return(allPoints[arrIndex]->checkOccupied());
    }
};

int main()
{
    GameTile tile1;
    GameTile tile2;

    Point tester1;
    Point tester2;

    //pass the addresses of tester1/tester2 here...
    tile1.addElement(0,&tester1);
    tile1.addElement(1,&tester2); // tile contains 2 points

    //and here...
    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;
}
Haha, thank you for checking back! :-)

This works perfectly, unfortunately I worked around this in what I believe to be a less efficient manner in the meantime, but this will benefit me in future projects, solved!
Thanks again! :-)
Last edited on
Topic archived. No new replies allowed.