简体   繁体   English

从'const char *'到'char'的无效转换类错误

[英]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 我有一个课程,而且我不知道如何解决.cc文件中的错误以进行编译

exerpt of .h file to show board in .h file .h文件的摘录以显示.h文件中的板

    class sudokuboard {

 private:

  /*** Member data ***/

  char board[9][9];

.cc file parts giving me trouble .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]’

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. 问题是C样式数组没有get方法。 The easiest solution whould be to access the variables with board[r][c] . 最简单的解决方案是使用board[r][c]访问变量。 But I would suggest using a c++ container. 但是我建议使用c ++容器。

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. 或者,如果您想更进一步,则可以将Matrix set一个类,以便可以使用xy坐标实现自己的getset函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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