1

So here is my code and my error is error C2512: 'std::array<std::array<SudokuGrid::Cell,9>,9>' : no appropriate default constructor I thought I was providing that with my public definition but I must be missing something. I have tried to integrate the answer from this question but I can't get the proper method

class SudokuGrid
{
private:
    struct Cell{
        int value; 
        bitset<9> pencils; 
        bool isSolved; 
        Cell(int i, bitset<9> p, bool s):
            value{ i = 0 },
            pencils{ p.reset() },
            isSolved{ s = false }{}
    };
    array < array < Cell, 9>, 9 > _grid;

public:
    SudokuGrid(string s) :_grid{}
    {
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
            {
                bitset<9> p;
                p.reset();
                _grid[i][j] = Cell(0, p, false);
            }   
    }
};
Community
  • 1
  • 1
CodeMonkey
  • 268
  • 5
  • 16
  • The expression `*new Cell(0, p, false);` creates a *memory leak*, you allocate memory and dereference the returned pointer then just throw away the pointer. Instead do e.g. `_grid[i][j] = Cell(...);`. – Some programmer dude Apr 19 '15 at 09:49
  • Also, when posting question about build errors, please include the complete and unedited build log in the question, and mark out where the error is in the code shown. Please edit your question to include that information. – Some programmer dude Apr 19 '15 at 09:50
  • 1
    However, think about that `std::array` is very much like a normal array, and so the objects in the array will be created when the array is created, and so you need a *default* constructor. if you don't do any special construction of the array. – Some programmer dude Apr 19 '15 at 09:51

1 Answers1

2

The default constructor of std::array default constructs the elements it contains. Therefore, SudokuGrid::Cell must have a default constructor:

Cell():
    value(0),
    pencils(),
    isSolved(false){}

The full code is available at: http://goo.gl/CdpCH6

m.s.
  • 16,063
  • 7
  • 53
  • 88