Home > other >  C2397: Conversation from 'int' to 'bool' requires a narrowing conversation
C2397: Conversation from 'int' to 'bool' requires a narrowing conversation

Time:01-04

I am trying to create a Sudoku board, and have a pointer reflecting to it. I did this by making a 2D array out of a struct that contained the number on that area of the board, and a boolean on whether it was placed there by the program or the player, so that later they will be able to remove the number if they place it incorrectly.

When the grid is filled with 0's, the error doesn't occur, but as soon as a number is added, it flags up with error C2397.

My code at the moment is:

struct sudokuNumbers {
    int number;
    bool solid = true;
};

class SudokuBoard {
private:
    sudokuNumbers sudokuBoard1[9][9]
    {
        5, 3, 4, 6, 7, 8, 9, 1, 2,
        6, 7, 2, 1, 9, 5, 3, 4, 8,
        1, 9, 8, 3, 4, 2, 5, 6, 7,
        8, 5, 9, 7, 6, 1, 4, 2, 3,
        4, 2, 6, 8, 5, 3, 7, 9, 1,
        7, 1, 3, 9, 2, 4, 8, 5, 6,
        9, 6, 1, 5, 3, 7, 2, 8, 4,
        2, 8, 7, 4, 1, 9, 6, 3, 5,
        3, 4, 5, 2, 8, 6, 1, 7, 9,
    };
};

I just don't understand what the program is trying to convert, because if I hover my cursor over the array it comes up showing that every value in the board has the int value assigned to it and the bool value as true.

CodePudding user response:

Aggregate Initialization is too simple to do what you need done. It wants to initialize EVERYTHING, including the bool member with the default initializer. The compiler gets upset when it sees a bool being initialized with 3. It will gleefully accept 0 (false) and 1 (true), explaining why the all zero version of your initializer was accepted.

The simple fix is to add a constructor to sudokuNumbers that only accepts the number.

struct sudokuNumbers {
    int number;
    bool solid = true;
    sudokuNumbers(int num): number(num) {} // new constructor
};

class SudokuBoard {
private:
    sudokuNumbers sudokuBoard[9][9] // uses new constructor
    {
        5, 3, 4, 6, 7, 8, 9, 1, 2,
        6, 7, 2, 1, 9, 5, 3, 4, 8,
        1, 9, 8, 3, 4, 2, 5, 6, 7,
        8, 5, 9, 7, 6, 1, 4, 2, 3,
        4, 2, 6, 8, 5, 3, 7, 9, 1,
        7, 1, 3, 9, 2, 4, 8, 5, 6,
        9, 6, 1, 5, 3, 7, 2, 8, 4,
        2, 8, 7, 4, 1, 9, 6, 3, 5,
        3, 4, 5, 2, 8, 6, 1, 7, 9
    };
};

CodePudding user response:

Your struct is an int followed by a bool, so when you initialize your array it should look like this:

struct sudokuNumbers {
int number;
bool solid = true;
};

class SudokuBoard {
private:
  sudokuNumbers sudokuBoard1[9][9]
  {
       5,0, 3,0, 4,0, 6,0, 7,0, 8,0, 9,0, 1,0, 2,0,
    6,0, 7,0, 2,0, 1,0, 9,0, 5,0, 3,0, 4,0, 8,0,
    1,0, 9,0, 8,0, 3,0, 4,0, 2,0, 5,0, 6,0, 7,0,
    8,0, 5,0, 9,0, 7,0, 6,0, 1,0, 4,0, 2,0, 3,0,
    4,0, 2,0, 6,0, 8,0, 5,0, 3,0, 7,0, 9,0, 1,0,
    7,0, 1,0, 3,0, 9,0, 2,0, 4,0, 8,0, 5,0, 6,0,
    9,0, 6,0, 1,0, 5,0, 3,0, 7,0, 2,0, 8,0, 4,0,
    2,0, 8,0, 7,0, 4,0, 1,0, 9,0, 6,0, 3,0, 5,0,
    3,0, 4,0, 5,0, 2,0, 8,0, 6,0, 1,0, 7,0, 9,0,
   
   };
};
  • Related