简体   繁体   English

在 C++17 中初始化整数向量的向量

[英]Initializing a vector of vectors of ints in C++17

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 使用 boost 分配初始化具有固定大小的向量的向量

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.由于它已有 7 年历史,我认为这可能与 C++17 的变化有关,但我不确定问题出在哪里。 The error the IDE tells me is "expected a type specifier" on the first argument. IDE 告诉我的错误是第一个参数上的“预期类型说明符”。 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));
};

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() { }

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

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