[英]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 set
一個類,以便可以使用x
和y
坐標實現自己的get
和set
函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.