gpt4 book ai didi

c++ - 重载运算符 << 后打印二维数组

转载 作者:行者123 更新时间:2023-11-28 01:14:06 25 4
gpt4 key购买 nike

我正在尝试重载运算符 << 并将其重定向到另一个函数。我正在尝试打印二维数组,但触发器似乎不起作用。

friend ostream& operator<<(ostream &out, const Board &c)
{
for (int i = 0; i < c.n; i++) {
for (int j = 0; j < c.n; j++) {
std::cout << c.board[i][j] << 1; // trying to debug this is not part of the code.
out << c.board[i][j];
}
out << endl;
}
return out;
}

我就是这样调用它的。 cout << board << endl; /* Shows an empty board

源.cpp

#include "Board.h"

#include <iostream>
using namespace std;

int main() {
Board board{ 3 }; // Initializes a 3x3 board
cout << board << endl; /* Shows an empty board:
....
....
....
....
*/

return 0;
}

棋盘.h

    #pragma once
#include <iostream>
using namespace std;

class Board {
private:
int n; // size
char** board; // matrix
public:
Board(int n = 3); // constructor
Board(const Board& another); // copy contructor
Board(Board&& another); // move constructor
~Board();
const Board& operator=(const Board& another) {
if (this != &another) {
n = another.n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = another.board[i][j]; // take values from original board into the new board
}
}
}
return *this;
}
friend ostream& operator<<(ostream &out, const Board &c)
{
for (int i = 0; i < c.n; i++) {
for (int j = 0; j < c.n; j++) {
std::cout << c.board[i][j] << 1; // trying to decug this is not part of the code.
out << c.board[i][j];
}
out << endl;
}
return out;
}
};

板.cpp

#include "Board.h"
#include <iostream>

Board::Board(int n) {
n = (n >= 3) ? n : 3;

char** board = new char*[n];
for (int i = 0; i < n; i++){
board[i] = new char[n];
}

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
board[i][j] = '.';
}
Board::Board(const Board& another) { // copy contructor
n = another.n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = another.board[i][j]; // take values from original board into the copy board
}
}
}
Board::Board(Board&& another) { // move constructor
n = another.n;
board = another.board; // copy by reference
}
Board::~Board(){
for (int i = 0; i < n; i++) {
delete[] board[i];
}
delete[] board;
}

顺便说一句:我试图通过 pastebin 链接上传代码,但编辑器不允许我这样做。

谢谢,我希望我已经足够清楚了。

最佳答案

你有几个问题。首先,在你的构造函数中你有

char** board = new char*[n];

此声明隐藏了您拥有的类成员 board。这意味着当构造函数退出时,board 类成员处于未初始化状态。你需要的只是

board = new char*[n];

解决这个问题。

您的第二个问题与构造函数中的 n 相同。您有一个名为 n 的构造函数参数,它隐藏了类的 n 成员。像访问类的 n

this->n = (n >= 3) ? n : 3;

或者只是将 Board::Board(int n) 更改为类似 Board::Board(int n_) 的内容,然后您将使用

n = (n_ >= 3) ? n_ : 3;

你的第三个问题是你的拷贝构造函数。它不分配任何内存,因此您对 board 所做的所有写入都是未定义的行为。您需要更改为

Board::Board(const Board& another) { // copy contructor
n = another.n;
board = new char*[n];
for (int i = 0; i < n; i++){
board[i] = new char[n];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = another.board[i][j]; // take values from original board into the copy board
}
}
}

让它正常工作。

最后,您的移动构造函数不会将移动到的对象设置为可删除状态。你需要

Board::Board(Board&& another) { // move constructor
n = another.n;
board = another.board; // copy by reference
another.board = nullptr; // null out moved from object
}

这样做。

关于c++ - 重载运算符 << 后打印二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59274444/

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