gpt4 book ai didi

c++ - 如何让一个类 cout 作为其数组属性之一

转载 作者:行者123 更新时间:2023-11-30 01:37:02 26 4
gpt4 key购买 nike

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

我从here发现如何打印数组。我从here发现如何将类对象作为其属性之一获取到 cout。我从 here 中发现cout 只有在它可以作为 friend 访问类的属性时才有效。

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

#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++) {

关于c++ - 如何让一个类 cout 作为其数组属性之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50478770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com