如何将 class 作为其数组属性之一进行计算

How to get a class to cout as one of its array properties

我是 C++ 的新手(我的常用语言是 Python)。

我从 here how to print an array. I found out from here how to get a class object to cout as one of its properties. And I found out from here 那里发现 cout 只有在它可以作为 friend 访问 class 的 属性 时才有效。

但是,当我合并答案时,它似乎不起作用。 这是我得到的:

#include <iostream>
using namespace std;

class TicTacToeGame {
    int board[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};

    friend std::ostream &operator<<(std::ostream &os, TicTacToeGame const &m);
};

std::ostream &operator<<(std::ostream &os, TicTacToeGame const &m) {
    for (int i = 0; i++; i < 9) {
        os << m.board[i];
    }
    return os;
}

int main()
{
    TicTacToeGame game;
    cout << game;
    return 0;
}

并且屏幕上没有打印任何内容。

我想看到的是 {0, 0, 0, 0, 0, 0, 0, 0, 0} 的内容,但只要我能看到数组,就不需要花哨的格式。

我怎样才能做到这一点?

修复 for 循环。

for (int i = 0; i++; i < 9) {

应该是

for (int i = 0; i < 9; i++) {

感谢 再次提醒我如何进行 for 循环。 (我已经很久没有做这些了...)

这是我决定暂时使用的运算符函数的更高级版本,因此它的打印效果就像井字游戏板。

std::ostream &operator<<(std::ostream &os, TicTacToeGame const &m) {
    for (int i = 0; i < 9; i++) {
        os << m.board[i];
        if (i%3!=2) {
            os << " ";
        }
        if (((i+1) % 3) == 0) {
            os << "\n";
        }
    }
    return os;
}