简体   繁体   English

如何从自己类的成员函数中访问公共变量? (C ++)

[英]How to access public variables from within their own class's member functions? (C++)

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. 但是,这超出了范围。

Goban::Goban (constructor) works great. Goban :: Goban(构造函数)效果很好。 It successfully accesses and changes the values of both "board" and "isWhiteToPlay", as intended. 它可以按预期成功访问和更改“ board”和“ isWhiteToPlay”的值。

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". printBoard的代码在调用此类的程序中有效,但是现在我将其移至此处,尝试进行编译时遇到“未在此范围内声明'board'”的情况。 Neither board nor isWhiteToPlay escape the same fate in playMove. 董事会和isWhiteToPlay都无法在playMove中逃脱同样的命运。

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? 有没有一种简单的方法可以从Goban的函数中访问和修改Goban的变量? 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. 我不希望为了将其传递给函数然后返回它而不得不将board视为指针。 I think I could do it, but I can't imagine there's not a vastly more elegant way. 我想我可以做到,但是我无法想象没有一种更优雅的方式。

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 <= 8; j++)
  {
    for (int i = 0; i <=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 <= 8; j++)
  {
    for (int i = 0; i <= 8; i++)
    {
      if (board[j][i] == -1)
        { std::cout << " O"; }
      else if (board[j][i] == 1)
        { std::cout << " X"; }
      else
        { std::cout << " ."; }
    }
  }
}

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;
  }
}

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. 如果有人了解我的问题,我已经足够了解正确的搜索词,那么欢迎您链接到适当的现有stackoverflow线程。 Of course, I wouldn't complain about answers here, but no need to reinvent the wheel, and all that. 当然,我不会在这里抱怨答案,但是不需要重新发明轮子,以及所有这些。

You mean to have Goban::printBoard and Goban::playMove . 您的意思是拥有Goban::printBoardGoban::playMove As it is, you are simply declaring + defining free functions. 实际上,您只是在声明+定义自由函数。

eg 例如

void Goban::printBoard() 
{
}

Just like you have for your constructor: 就像您的构造函数一样:

Goban::Goban()
{
}

I assume when you say 我想当你说

printBoard's code worked when it was in the program which is calling this class printBoard的代码在调用此类的程序中时有效

You mean that it used to work when you had the code in the class declaration, but you've now moved them into a separate definition. 您的意思是,当您在类声明中包含代码时,它曾经可以工作,但是现在您将它们移到了单独的定义中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++-如何从指向类的指针访问成员变量/函数 - C++ - How to access member variables/functions from a pointer to a class 如何从C ++类中的非成员函数访问成员变量 - How to access member variables, from non member function within class C++ 如何使用C ++中的好友函数将成员变量从一个类访问到另一个类? - How do I access member variables from one class into other using friend functions in C++? 如何从 C++ 中的弱指针访问 class 成员函数? - How to access the class member functions from a weak pointer in C++? C ++使用来自类似虚拟公共类的成员函数 - C++ Using member functions from a similar virtual public class 访问Cython中C ++类的私有成员变量/函数 - Access to private member variables/functions of a C++ class in Cython C ++:从类外部访问公共成员函数 - C++: Access to a public member function from outside of a class 如何通过封闭 class 访问嵌套的 class 成员 function,其中所有成员在 Z6CE809EACF90BA125B40FA4BD9 - How to access nested class member function from enclosing class where all the members are public in c++? C ++类-使用公共​​变量并在类外部定义成员函数 - C++ Class - using public variables and defining member functions outside class 如何使用公共成员函数(派生类)访问私有成员变量? - How do I access private member variables with public member functions (derived class)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM