C++ 如何限制矩阵类仅获取';X'';O';或'';

C++ 如何限制矩阵类仅获取';X'';O';或'';,c++,c++11,visual-c++,C++,C++11,Visual C++,我有一个board类,可以制作N*N个chars板 class Cell { public: int row; int col; }; class Board { private: int size; char** matrix = nullptr; //many other class functions. char & operator[](const Cell& cellToChange) { if (cellToChan

我有一个board类,可以制作N*N个chars板

class Cell
{ 
public:
    int row; int col;
};


class Board {
private:
    int size;
    char** matrix = nullptr;
   //many other class functions.
char & operator[](const Cell& cellToChange) {
        if (cellToChange.row < size && cellToChange.col < size) {
            return matrix[cellToChange.row][cellToChange.col];
        }
        else {
            cout << "ERROR!" << endl;
        }
    }

它将矩阵中的这个位置更改为“X”和任何其他字符

我需要将此矩阵限制为仅“X”“O”或“.”

我不允许链接主目录!我只能更改类别

我的目标,我不能实现的权利,不是让程序打印“错误”,当我试图这样做

"board1[{1, 4}] = 'z'".
为了实现这一目标,我已经浪费了好几个小时,我真的需要你的帮助

这是我写的全部课程:

#include <iostream>
using namespace std;


class Cell
{ 
public:
    int row; int col;
};


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
        if (this != &other) {
            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];
                }
            }
        }
    }

    Board(Board&& other) {                   //move constructor
        size = other.size;
        matrix = other.matrix;
        other.matrix = nullptr;
    }



    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;
        }
        os << endl;
        return os;
    }


    char & operator[](const Cell& cellToChange) {
        if (cellToChange.row < size && cellToChange.col < size) {
            return matrix[cellToChange.row][cellToChange.col];
        }
        else {
            cout << "ERROR!" << endl;
        }
    }

    void operator=(char charToAdd) {
        if (charToAdd == 'X' || charToAdd == 'O' || charToAdd == '.') {
            for (int i = 0; i < size; i++) {         
                for (int j = 0; j < size; j++) {
                    matrix[i][j] = charToAdd;
                }
            }
        }
        else {
            cout << "ERROR!" << endl;
        }
    }

    const Board& operator=(const Board& other) {
        if (this != &other) {
            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];
                }
            }
        }
        return *this;
    }
};

#包括
使用名称空间std;
类单元
{ 
公众:
int行;int列;
};
班级委员会{
私人:
整数大小;
字符**矩阵=空PTR;
公众:
电路板(int sizeToSet){//具有大小的构造函数
大小=大小设置;
矩阵=新字符*[size];//创建一个矩阵
对于(int i=0;i

代理运算符[](常量单元格和单元格更改){
if(cellToChange.row
您需要的是另一层抽象。由于您无法控制赋值的右侧,因此您需要控制赋值运算符。为此,您需要一个代理对象。您将从
运算符[]
返回该对象,然后在其赋值运算符中执行逻辑。这看起来像

class Proxy
{
    char& val;
    Proxy(char& val) : val(val) {}
    Proxy& operator=(char new_val)
    {
        if (new_val == 'X' || new_val == 'O' || new_val == '.')
        {
            val = new_val;
            return *this;
        }
        throw std::invalid_argument("Invalid Assignment.  Use X, O, or .");
    }
    // allows this class to implicitly convertible to a char
    operator char() { return val; }
};

代理运算符[](常量单元格和单元格更改){
if(cellToChange.row
非常感谢!它工作了!但它不会打印我的错误。它只是不接受错误。你能解释一下原因吗?@Dima我更新了代码,如果发生无效的分配,就会抛出异常。非常感谢!你经过数小时的尝试终于救了我!非常感谢!它工作了!但它不会打印我的错误。它只是不接受错误是的。你能解释一下原因吗?@Dima我更新了代码,如果发生无效的赋值,就会抛出异常。非常感谢!经过几个小时的尝试,你终于救了我!
class Proxy
{
    char& val;
    Proxy(char& val) : val(val) {}
    Proxy& operator=(char new_val)
    {
        if (new_val == 'X' || new_val == 'O' || new_val == '.')
        {
            val = new_val;
            return *this;
        }
        throw std::invalid_argument("Invalid Assignment.  Use X, O, or .");
    }
    // allows this class to implicitly convertible to a char
    operator char() { return val; }
};
Proxy operator[](const Cell& cellToChange) {
    if (cellToChange.row < size && cellToChange.col < size) {
        return {matrix[cellToChange.row][cellToChange.col]};
    }
    throw std::out_of_range("invalid index");
}