title: How to access public variables from within their own class's member functions? (C++)
body: <p>I would like to access a public variable I declared at the beginning of my class from one of its member functions. But it's saying it's out of scope.</p>
  
  <p>Goban::Goban (constructor) works great. It successfully accesses and changes the values of both "board" and "isWhiteToPlay", as intended.</p>
  
  <p>printBoard's code worked when it was in the program which is calling this class, but now that I've moved it here, attempts to compile are met with "‘board’ was not declared in this scope". Neither board nor isWhiteToPlay escape the same fate in playMove.</p>
  
  <p>What am I doing wrong here? It's a public variable, but it shouldn't even need to be since I'm still in the class, right? Is there an easy way to access and modify Goban's variables from within Goban's functions? I would not look forward to having to treat board as a pointer in order to pass it to the function and then return it. I think I could do it, but I can't imagine there's not a vastly more elegant way.</p>
  
  <pre><code>class Goban
      {
      public:
        int board[9][9]; // y,x  -1 = W  +1 = B
        int captures[2];
        bool isWhiteToPlay; // 0 for Black's turn, 1 for White's turn
  
    Goban();
    void printBoard(); // Prints the board to the terminal wherefrom the program is called.
    void playMove(int x, int y); // Makes a move for the turn player at the coordinate (x,y). Right now I'm just trying to get it to change the value of the proper coordinate of "board" and not caring if the move is legal.
  };
  
  Goban::Goban() // Just initializing everything to zero here for now. Interesting to note that there are no compiling errors in this constructor. But later it complains when I use board and isWhiteToPlay elsewhere. The only difference I can see is that here they're in for loops, and there they're in if clauses. Not sure why that would make a difference, nor how to work around it.
  {
    captures[0] = 0;
    captures[1] = 0;
  
    for (int j = 0; j &lt;= 8; j++)
    {
      for (int i = 0; i &lt;=8; i++)
      {
        board[j][i] = 0;
      }
    }
  
    isWhiteToPlay = 0;
  }
  
  void printBoard() // This code worked correctly when it was in the program, but the problem started when I moved it here to the class.
  {
    for (int j = 0; j &lt;= 8; j++)
    {
      for (int i = 0; i &lt;= 8; i++)
      {
        if (board[j][i] == -1)
          { std::cout &lt;&lt; " O"; }
        else if (board[j][i] == 1)
          { std::cout &lt;&lt; " X"; }
        else
          { std::cout &lt;&lt; " ."; }
      }
    }
  }
  
  void playMove(int x, int y) // Same errors as above; solution is probably the same.
  {
    if (isWhiteToPlay == 0)
    {
      board[y][x] = -1;
      isWhiteToPlay = 1;
    }
    else
    {
      board[y][x] = 1;
      isWhiteToPlay = 0;
    }
  }
  </code></pre>
  
  <p>I suspect someone has probably already asked this question, but I think I'm just not coming up with the right search terms, which is probably an indication that I don't fully understand what I'm doing wrong here. If anyone understands the problem I have well enough to know the right search terms, a link to the appropriate extant stackoverflow thread would be welcome. Of course, I wouldn't complain about answers here, but no need to reinvent the wheel, and all that.</p>