class 范围内的二维向量
2-dimensional vector in class scope
我想要一个包含字符串的二维向量,所以我尝试了类似的方法:
QVector<QVector<QString*> > register_options(8, QVector<QString*>(8)); //getting 'expected identifier before numeric constant'
QVector<QVector<QString> > register_options; // getting 'field has incomplete type'
这是我找到的初始化here
我还读到在 class 范围内不可能没有初始化向量,应该在构造函数中完成,我理解但是我无法在 class 中定义 var 的原型范围。
我也试过了
//header
QVector<QString> register_bits; // field has incomplete type
QVector<QVector<QString> > register_options; // field has incomplete type
//source
registers::registers() : register_bits(8, 0), register_options(8, register_bits)
{ //...
所以我的问题是:如何在class范围内定义向量,然后在构造函数中初始化它?
So my question is: how to define the vector in the class scope and
then initialize it in the constructor?
使用 C++ 11 初始化列表在内存中初始化此类结构的一种可能方法:
class MyClass
{
private:
QVector<QVector<QString> > vct {{"", ""}, {""}, {"", "", ""}};
};
或行:
QVector<QVector<QString> > vct = {{"", ""}, {""}, {"", "", ""}};
或在构造函数或任何 class' 方法中:
vct = {{"", ""}, {""}, {"", "", ""}};
我试过了,编译成功了。上面的模式实际上是 aggregate initialization 正如原始发帖人在评论中所问的那样。
我想要一个包含字符串的二维向量,所以我尝试了类似的方法:
QVector<QVector<QString*> > register_options(8, QVector<QString*>(8)); //getting 'expected identifier before numeric constant'
QVector<QVector<QString> > register_options; // getting 'field has incomplete type'
这是我找到的初始化here
我还读到在 class 范围内不可能没有初始化向量,应该在构造函数中完成,我理解但是我无法在 class 中定义 var 的原型范围。
我也试过了
//header
QVector<QString> register_bits; // field has incomplete type
QVector<QVector<QString> > register_options; // field has incomplete type
//source
registers::registers() : register_bits(8, 0), register_options(8, register_bits)
{ //...
所以我的问题是:如何在class范围内定义向量,然后在构造函数中初始化它?
So my question is: how to define the vector in the class scope and then initialize it in the constructor?
使用 C++ 11 初始化列表在内存中初始化此类结构的一种可能方法:
class MyClass
{
private:
QVector<QVector<QString> > vct {{"", ""}, {""}, {"", "", ""}};
};
或行:
QVector<QVector<QString> > vct = {{"", ""}, {""}, {"", "", ""}};
或在构造函数或任何 class' 方法中:
vct = {{"", ""}, {""}, {"", "", ""}};
我试过了,编译成功了。上面的模式实际上是 aggregate initialization 正如原始发帖人在评论中所问的那样。