如何使board1 [{1,1}] ='X';大学工作

时间:2019-12-05 12:20:20

标签: c++

我需要使这一行在我的主要功能board1[{1,1}]='X';中起作用。 我不知道如何使它工作。我想得到一些帮助。 这是我的董事会课程:

class Board {
private:
    int size;
    char** matrix = nullptr;

public: 

    Board(int sizeToSet) {                       //constructor with size
        size = sizeToSet;

        matrix = new char*[size];                 //creates a matrix
        for (int i = 0; i < size; i++)
            matrix[i] = new char[size];

        for (int i = 0; i < size; i++) {          //makes every cell in matix '.'
            for (int j = 0; j < size; j++) {
                matrix[i][j] = '.';
            }
        }
    }

    void printSize() {                            //matrix size print
        cout << size << endl;
    }

    ~Board() {                                    //destructor
        for (int i = 0; i < size; i++)
            delete[] matrix[i];
        delete[] matrix;
    }

    Board(const Board& other) {                   //copy constructor
        size = other.size;
        matrix = new char*[size];

        for (int i = 0; i < size; i++)
            matrix[i] = new char[size];

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                matrix[i][j] = other.matrix[i][j];
            }
        }
    }

    friend ostream& operator<<(ostream& os, const Board& boardToPrint) {       //prints matrix
        for (int i = 0; i < boardToPrint.size; i++) {
            for (int j = 0; j < boardToPrint.size; j++) {
                os << boardToPrint.matrix[i][j] << "  ";
            }
            os << endl;
        }
        return os;
    }
    int operator()(int row, int col) {
        cout << "it worked1" << endl;
        return 1;

    }
};

3 个答案:

答案 0 :(得分:1)

要对operator[]进行成对或类似重载

char & operator[]( const std::pair<size_t, size_t> &pair ) {
    return matrix[pair.first][pair.second]
}

然后您就可以通过调用此功能

Board board1{10};
// …
board1[{0,0}] = 'X';

由于std::pair可以用{x,y}初始化,其中xy是数字。

答案 1 :(得分:1)

如果您不能使用std :: pair <>(由于学术上的限制),则可以制作自己的简单结构来实现相同的目的。

struct mypair { int first; int second;};

然后添加

char & operator[]( const mypair & st ) {
   return matrix[st.first][st.second];
}

和main()

Board board1{10};
board1[{0,0}] = 'X';
return 0;

完整示例如下: https://ideone.com/mdkzM5

或者如果链接腐烂在这里:

#include <iostream>
using namespace std;

struct mypair { int first; int second;}; 

class Board {
private:
    int size;
    char** matrix = nullptr;

public: 

    Board(int sizeToSet) {                       //constructor with size
        size = sizeToSet;

        matrix = new char*[size];                 //creates a matrix
        for (int i = 0; i < size; i++)
            matrix[i] = new char[size];

        for (int i = 0; i < size; i++) {          //makes every cell in matix '.'
            for (int j = 0; j < size; j++) {
                matrix[i][j] = '.';
            }
        }
    }

    void printSize() {                            //matrix size print
        cout << size << endl;
    }

    ~Board() {                                    //destructor
        for (int i = 0; i < size; i++)
            delete[] matrix[i];
        delete[] matrix;
    }

    Board(const Board& other) {                   //copy constructor
        size = other.size;
        matrix = new char*[size];

        for (int i = 0; i < size; i++)
            matrix[i] = new char[size];

        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                matrix[i][j] = other.matrix[i][j];
            }
        }
    }

    friend ostream& operator<<(ostream& os, const Board& boardToPrint) {       //prints matrix
        for (int i = 0; i < boardToPrint.size; i++) {
            for (int j = 0; j < boardToPrint.size; j++) {
                os << boardToPrint.matrix[i][j] << "  ";
            }
            os << endl;
        }
        return os;
    }
    int operator()(int row, int col) {
        cout << "it worked1" << endl;
        return 1;

    }

    char & operator[]( const mypair & st ) {
        return matrix[st.first][st.second];
    }
};


int main() {
    Board board1{10};
    board1[{0,0}] = 'X';

    std::cout << board1;

    return 0;
}

答案 2 :(得分:0)

重载索引运算符以采用合适的对:

char& operator[](const std::pair<int, int>& xy)
{
    return matrix[xy.first][xy.second];
}