我试图使运算符<<重载,并将其重定向到另一个函数。我正在尝试打印一个二维数组,但似乎触发器不起作用。
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
Source.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;
}
Board.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;
}
};
Board.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链接上,但是编辑器不允许我这么做。
谢谢我希望我足够清楚。
答案 0 :(得分:2)
您遇到了一些问题。首先,在您的构造函数中,您已经
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
}
}
}
使其正常工作。
最后,您的move构造函数不会将move到object设置为可删除状态。您需要
Board::Board(Board&& another) { // move constructor
n = another.n;
board = another.board; // copy by reference
another.board = nullptr; // null out moved from object
}
做到这一点。