3

I have tried creating a vector of vectors in a class with a fixed size using the solution from the following thread but to no avail. Initializing a vector of vectors having a fixed size with boost assign

Since it's 7 years old, I'm thinking it could be something to do with C++17 changes, but I'm unsure where the issue is otherwise. The error the IDE tells me is "expected a type specifier" on the first argument. Having a look at the documentation for constructors, nothing seems wrong, unless I missed something.

class SudokuSolver {

public:
    SudokuSolver() {}

    ~SudokuSolver() {}

private:
    std::vector<std::vector<int>> sudoku_field(9, std::vector<int>(9, 0));
};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Jack Avante
  • 1,405
  • 1
  • 15
  • 32

1 Answers1

5

You can use squiggly brackets to let the compiler know you're trying to call a constructor:

std::vector<std::vector<int>> sudoku_field{9, std::vector<int>(9, 0)};

Alternatively, you could do this work in the initialization list of your default constructor:

SudokuSolver() : sudoku_field(9, std::vector<int>(9, 0)) {}

And then run your default constructor from every new constructor you make to ensure that gets set:

SudokuSolver(int thing) : SudokuSolver() { }
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Thanks so much. I keep forgetting that that is a quirk C++ has. I remember what the problem is now. – Jack Avante Oct 07 '20 at 18:01
  • 5
    This works in this case but using that notation is risky with `std::vector` because it will try its hardest to interpret it as a list of element. It will only use another constructor if it is impossible to interpret it as a list of elements. – François Andrieux Oct 07 '20 at 18:03