2017-10-12 116 views
2

我有一個類,我不知道如何在.cc文件解決錯誤編譯從「爲const char *」到「炭」級錯誤無效的轉換

exerpt .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]’ 

我不知道該怎麼再更改。我嘗試了很多不同的方法。

+0

數組中沒有'get'方法,所以'board.get'應該只是'sudokuboard'的'get'方法。 ''_「'是一個字符串文字,它應該是''_''。 – VTT

回答

0

問題是C風格的數組沒有get方法。 最簡單的解決方案是使用board[r][c]來訪問變量。 但我會建議使用c + +容器。

using Row = std::vector<char>; 
using Matrix = std::vector<Row>; 

Matrix board; 

或者,如果你想採取了一步,可以使基類,所以你可以實現自己的getset功能服用xy協調。

相關問題