我试图重载运算符<

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;
}


董事会

    #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链接上,但是编辑器不允许我这样做。

谢谢,我希望我足够清楚。

最佳答案

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

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
}


要做到这一点。

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

10-09 19:16