How to set initialize size of std::vector?

Write your question here.

I am attempting to use vector "fill constructor" to push_back 4 instances of Map.
But got a compile error on the last line below. What is the correct syntax?

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <vector>
#include <string>
#include <iostream>

struct Map
{
    unsigned int mat=32;
    unsigned int row=32;
    unsigned int col=32;
};

struct Row
{
    std::string scanner;
    std::string pin;
    std::vector<Map> vLayoutToKeyMatrix;
};

struct RowInv
{
    std::vector<Map> vKeyMatrixToLayout;
};

struct Matrix
{
    std::vector<Row> vRows;
    std::vector<RowInv> vRowsInv;
};

std::vector<Matrix> vMatrices;

int main()
{
    vMatrices.push_back(Matrix());

    //instantiate vRows
    vMatrices[0].vRows.push_back(Row());
    vMatrices[0].vRows[0].vLayoutToKeyMatrix.push_back(Map());

    //instantiate vRowsInv
    vMatrices[0].vRowsInv.push_back(RowInv());
    vMatrices[0].vRowsInv[0].vKeyMatrixToLayout.push_back(Map(4));
        //error: expected primary-expression before ‘Map’
}
1
2
// vMatrices[0].vRowsInv[0].vKeyMatrixToLayout.push_back(Map(4));
vMatrices[0].vRowsInv[0].vKeyMatrixToLayout.resize(4);
Thanks JLBorges, that compiled nice!
Topic archived. No new replies allowed.