Accessing a function in "private:"

I have a problem accessing a function in my "private:", named void display();

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
class Board
{
private:
    Piece list[64];
    int x1, y1;
    int status, redpiece, greenpiece, winner;
    int inRange(int x, int y);
    int pieceAtLoc(int x, int y);
    int validPlacement(int x, int y);
    void flip(int x, int y);
    void display();
    void move();
public:
    Board();
    void execute();
};
Board::Board()
{
    list[0].setposition(4,4,0);
    list[1].setposition(5,4,1);
    list[2].setposition(4,5,0);
    list[3].setposition(5,5,1);
}
void Board::execute()
{
    Board::display()
    {
        cout<<"hello"<<endl;
    }
}
int main()
{
    Board b;
    b.execute();
}

Can someone tell me how to access display correctly?...Thank You!
1
2
3
4
5
6
7
8
9
10
11
void Board::execute()
{
    this->display();
    // or
    display(); // "this->" is implicit on this line.
}

void Board::display()
{
    std::cout<<"hello"<<std::endl;
}
You need to move the definition of Board::display() out of your Board::execute() function:
1
2
3
4
5
6
void Board::display()
{
    cout << "hello" << endl;
}
void Board::execute()
// ... 


Then to call display(), you just call it like any other function:
1
2
3
4
void Board::execute()
{
    display();
}
thanks long double main!..
Last edited on
I tried doing it but it gives me a error for "operator<<"

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
class Board
{
private:
    Piece list[64];
    int x1, y1;
    int status, redpiece, greenpiece, winner;
    int inRange(int x, int y);
    int pieceAtLoc(int x, int y);
    int validPlacement(int x, int y);
    void flip(int x, int y);
    void display();
    void move();
public:
    Board();
    void execute();
};
Board::Board()
{
    list[0].setposition(4,4,0);
    list[1].setposition(5,4,1);
    list[2].setposition(4,5,0);
    list[3].setposition(5,5,1);
}
void Board::display()
{
    for(int i=0; i<64; i++)
    {
        if((i==8)||(i==16)||(i==24)||(i==32)||(i==40)||(i==48)||(i==56)||(i==64))
        {
            cout<<endl;
            cout<<list[i];
        }
        else
        {
            cout<<list[i];
        }

    }
}
void Board::execute()
{
    display();
}
int main()
{
    Board b;
    b.execute();
}
In order to do cout << list[i];, you'll need to define operator<< for Piece.

If you don't want to/don't feel like doing that, then you'll have to display the members of Piece somehow (I don't know what your Piece class looks like, so I don't know what cout << somePiece; should look like).
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
class Piece
{
private:
    int x,y;
    char color;
public:
    Piece()
    {
        x=-1;
        y=-1;
        color=2;
    }
    void setcolor(char c)
    {
        color=c;
    }
    void setposition(int x_pos,int y_pos, char c)
    {
        x=x_pos;
        y=y_pos;
        color=c;
    }
    void set(int x_pos, int y_pos)
    {
        x=x_pos;
        y=y_pos;
    }
    int getc()
    {
        return color;
    }
    int getx()
    {
        return x;
    }
    int gety()
    {
        return y;
    }
    void display();
};


What if, instead of cout << list[i];, you just did list[i].display();?
It brings an error that Piece::display() is undefined. But if you can give me a few minutes I finish some code on Piece::display()...hopefully it works
I need to figure out how to make display as if it was on a board but here is the raw code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Piece::display()
{
    int a=getc();
    if(a==0)
    {
        cout<<"O ";
    }
    else if(a==1)
    {
        cout<<"X ";
    }
    else
    {
        cout<<"  ";
    }
}
Don't call that function "getc".
getc is an actual function existing in the C standard. Call it getcolor or similar, it's hard to understand it on-the-fly for me.
Besides, it still works.

As to make a grid, you should edit Board::display, but as I'm on mobile it's hard to say more right now.
Thank you! For the heads and if you could get back to me on the grid thing that would be great!
I have to think it's a 8x8 board?

In this case:

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
void Board::display()
{
    const int board_width = 8;
    const int board_height = 8;
    const int board_size = board_width*board_height;
    for(int i=0; i<board_size; i++)
    {
        int x = i%board_width; // result is in range 0-7. 0 being the leftmost, 7 the rightmost.
        int y = (i-x)/board_width; // result is also in range 0-7. 0 being the top, 7 the bottom.
        if(i && (!x) )
        {
            cout<<endl; // If "i" is not 0 and the X position is 0, go down one line
// if the Y position equals to the last Y position, don't write a horizontal separator
            if(y != (board_height-1))
            {
                for(int x = 0; x < board_width; ++x)
                    cout<<"---";
// They are three lines, because two spaces are used in Piece::display, and one for vertical separators
            }
        }
        list[i].display();
// If the X position equals to the last X position, don't write a vertical separator
        if(x != (board_width-1))
            cout<<'|';
    }
}


Didn't test but should work.
To change the board's size you can just change board_width and board_height.
Last edited on
Topic archived. No new replies allowed.