简体   繁体   中英

invalid conversion from ‘const char*’ to ‘char’ Class Error

I have a class and I don't know how to solve an error in the .cc file to compile

exerpt of .h file to show board in .h file

    class sudokuboard {

 private:

  /*** Member data ***/

  char board[9][9];

.cc file parts giving me trouble

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

I don't know what to change anymore. i've tried so many different approaches.

The problem is that a C-style array doesn't have a get method. The easiest solution whould be to access the variables with board[r][c] . But I would suggest using a c++ container.

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

Matrix board;

Or if you want to take it a step further, you can make Matrix a class so you can implement your own get and set functions taking an x and a y coordinate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM