作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我是一名优秀的程序员,十分优秀!