从“常量字符*”到“字符”类错误的转换无效
invalid conversion from ‘const char*’ to ‘char’ Class Error
>我有一个类,我不知道如何解决.cc文件中的错误进行编译
执行.h文件以在.h文件中显示板
class sudokuboard {
private:
/*** Member data ***/
char board[9][9];
.cc 文件部分给我带来麻烦
sudokuboard::sudokuboard()
{
for (size_t r = 0; r < 9; r++){
for (size_t c = 0; c < 9; c++)
board[r][c] = '_';
}
}
void sudokuboard::print() const
// write the board to cout
{
for (size_t r = 0; r < 9; r++){
string colStr = "";
for (size_t c = 0; c < 9; c++){
colStr += board.get(r, c);
}
cout << colStr << endl;
}
void sudokuboard::remove(size_t r, size_t c)
// remove the numeral at position (r,c)
{
board[r][c] = "_";
}
ERRORS:
sudokuboard.cc: In member function ‘void sudokuboard::print() const’:
sudokuboard.cc:26: error: request for member ‘get’ in ‘((const
sudokuboard*)this)->sudokuboard::board’, which is of non-class type
‘const char [9][9]’
sudokuboard.cc: In member function ‘void sudokuboard::remove(size_t,
size_t)’:
sudokuboard.cc:42: error: invalid conversion from ‘const char*’ to ‘char’
sudokuboard.cc:59: error: request for member ‘get’ in ‘((const
sudokuboard*)this)->sudokuboard::board’, which is of non-class type ‘const
char [9][9]’
我不知道该改变什么了。 我尝试了很多不同的方法。
问题是 C 样式数组没有get
方法。最简单的解决方案是使用 board[r][c]
访问变量。但我建议使用 c++ 容器。
using Row = std::vector<char>;
using Matrix = std::vector<Row>;
Matrix board;
或者,如果你想更进一步,你可以使 Matrix 成为一个类,这样你就可以实现自己的get
,并set
函数采用x
和y
坐标。
相关文章:
- 我的字符计数代码计算错误.为什么
- 错误:字符数组的初始值设定项太多
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- C++ 字符错误
- CppLint铸造字符*错误
- R data.table 和 STRING_ELT() 错误:如何修复"字符向量"与"字符"错误?
- C URL用UTF8字符错误解码
- C++:如何在 do while 循环中解决字符错误
- 遍历字符* 错误没有从字符* 转换为 int
- Arduino SIM800L:将字符串转换为字符* 错误
- Qt 中的常量字符* 错误行为
- 返回c++中的字符错误数组
- C++:队列中的第一个字符错误
- c++ SFML 按键字符错误
- C++字符*错误,程序崩溃
- 输出字符串/字符错误(c++)
- 将输入字符串转换为字符* <错误 Ptr>
- 井字棋数组字符错误
- c++读取字符串字符错误
- CouchDB UTF-8字符错误