How to hard-code vector initialization for vector of struct vectors?

Dec 15, 2016 at 12:01pm
The following class gets this compile error:
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
In file included from scancodesTest.cpp:9:0:
ScancodeInstantiations.h: In constructor ‘ScancodeInstantiations::ScancodeInstantiations()’:
ScancodeInstantiations.h:35:17: error: no match foroperator=’ (operand types are ‘std::vector<Scancodes>’ and ‘<brace-enclosed initializer list>’)
                 };
                 ^
In file included from /usr/include/c++/6.2.1/vector:69:0,
                 from Layout.h:7,
                 from scancodesTest.cpp:8:
/usr/include/c++/6.2.1/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = Scancodes; _Alloc = std::allocator<Scancodes>]
     vector<_Tp, _Alloc>::
     ^~~~~~~~~~~~~~~~~~~
/usr/include/c++/6.2.1/bits/vector.tcc:167:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const std::vector<Scancodes>&’
In file included from /usr/include/c++/6.2.1/vector:64:0,
                 from Layout.h:7,
                 from scancodesTest.cpp:8:
/usr/include/c++/6.2.1/bits/stl_vector.h:450:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = Scancodes; _Alloc = std::allocator<Scancodes>]
       operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
       ^~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:450:7: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::vector<Scancodes>&&’
/usr/include/c++/6.2.1/bits/stl_vector.h:471:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = Scancodes; _Alloc = std::allocator<Scancodes>]
       operator=(initializer_list<value_type> __l)
       ^~~~~~~~
/usr/include/c++/6.2.1/bits/stl_vector.h:471:7: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::initializer_list<Scancodes>’
scancodesMakefile:14: recipe for target 'scancodesTest.o' failed
make: *** [scancodesTest.o] Error 1


Class
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
#ifndef SCANCODEINSTANTIATIONS_H
#define SCANCODEINSTANTIATIONS_H

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "file_functions.h"
#include "Layout.h"

struct Code
{
    std::vector<std::string> name;
    std::vector<std::string> scancode;
};

struct Scancodes
{
    std::string type;
    std::vector<Code> codes;
};

class ScancodeInstantiations
{
    private:
        std::vector<Scancodes> scancodes;
    public:
        ScancodeInstantiations()
        {
            scancodes =
                {     //type           name, scancode
                    { "Code_Shift", { {"s_shift", "MODIFIERKEY_LEFT_SHIFT"} } },
                    { "Code_Sc",    { {"s_a", "KEY_A"}, {"s_b", "KEY_B"} } }
                };
        }
        void generate(Layout layout);
};
#endif 

What is the proper way to hard-code vector initialization for vector of struct vectors?
I am using g++ (GCC) 6.2.1 20160916 (Red Hat 6.2.1-2).

Thank you.
Last edited on Dec 15, 2016 at 12:03pm
Dec 15, 2016 at 12:12pm
Your code would have worked if name and scancode were simply strings. Now they are vectors so you need to add an extra pair of curly brackets around them.

1
2
3
4
5
scancodes =
    {     //type            name, scancode
        { "Code_Shift", { {{"s_shift"}, {"MODIFIERKEY_LEFT_SHIFT"}} } },
        { "Code_Sc",    { {{"s_a", "KEY_A"}}, {{"s_b", "KEY_B"}} } }
    };
Last edited on Dec 15, 2016 at 12:14pm
Dec 15, 2016 at 12:21pm
Thanks Peter87. Name and scancode were suposed to be strings.
You saved the day.
Topic archived. No new replies allowed.