如何将矩阵类限制为仅获取"XO"或""。
How to limit a matrix class to get only 'X' 'O' or '.'
我有一个板类,可以制作 N*N 个字符板。
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;
}
}
现在当我主要使用这个
"board1[{1, 4}] = 'X';"
它将矩阵中的这个地方更改为"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;
}
};
这是不允许更改的全部主要内容:
#include "Board.h"
#include <iostream>
using namespace std;
int main() {
Board board1{4}; // Initializes a 4x4 board
cout << board1 << endl; /* Shows an empty board:
....
....
....
....
*/
cout << board1[{1,2}] << endl; // .
board1[{1,1}]='X';
board1[{1,2}]='O';
char c = board1[{1,2}]; cout << c << endl; // O
cout << board1 << endl; /* Shows the following board:
....
.XO.
....
....
*/
// This should raise an exception
// "Illegal"
board1 = '.'; // Fill the entire board with "."
cout << board1 << endl; /* Shows an empty board, as above */
board1 = 'a'; // This should raise exception
// "Illegal"
board1[{0,1}] = 'x'; // This should raise an exception
// "Illegal"
Board board2 = board1;
board2[{0,0}] = 'X';
cout << board1 << endl; /* Shows an empty board, as above */
cout << board2 << endl; /* Shows a board with an X at top-left */
board1 = board2;
board1[{3,3}] = 'O';
cout << board2 << endl; /* Shows a board with an X at top-left */
cout << board1 << endl; /* Shows a board with an X at top-left and O at bottom-right */
cout << "Good bye!" << endl;
return 0;
}
谢谢!
你需要的是另一层抽象。 由于您无法控制分配的右侧,因此需要控制分配运算符。 为此,您需要一个代理对象。 您将从operator[]
返回该对象,然后在其赋值运算符中执行逻辑。 那看起来像
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");
}
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 获取字符串的长度并将其分配给数组
- 无法获取菜单选择以运行函数.C++
- 数组长度,为什么从命令行获取时不能使用它?
- Boost Spirit,获取迭代器内部语义动作
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 具有默认值的引用获取函数
- xmake总是报告:错误:无法获取cxx的程序,为什么
- 如何将矩阵类限制为仅获取"XO"或""。