user8108550
user8108550

Reputation:

braces around scalar initializer error

I have these two structures

struct vino_t {
    int tip;
    char *sorta;
    int godina;
    int cena;
};

struct vinarija_t {
    char *ime_vinarija;
    char lokacija[50];
    int kolku_vina;
    vino_t *lista_vina;
    int zarabotka;
    int nagradi;
}

and I want to initialize an array of three elements of type vinarija_t (the second structure).

I have my list like this

vinarija_t lista_vinar[3] = {
            {"Bovin", "Negotino", 3, {{1, "zhilavka", 2015, 850},{1, "rkaciteli", 2017, 700},{2, "kratoshija", 2009, 900}},2450,4},
            {"Tikvesh", "Kavadarci", 3,{{2, "vranec", 2016, 750},{1, "smedervka", 2007, 1000},{3, "zinfandel", 2014, 850}},2600,3},
            {"Dalvina", "Bosilovo", 3,{{2, "barbera", 2002, 1200},{3, "merlo", 2017, 850},{3, "malbek", 2016, 700}},2750,5} };

and I constantly get the error: braces around scalar initializer for type vino_t*. I tried changing the braces, adding them, removing them, but somehow nothing solves my problem.

Upvotes: 1

Views: 1323

Answers (1)

NathanOliver
NathanOliver

Reputation: 180510

In vinarija_t, lista_vina is a vino_t *. Since it is a pointer it can only be initialized with a single value(a pointer holds a single address). If you want an array then you need to change lista_vina to be vino_t lista_vina[3];.

With that said, you might not want that. If you want an array that can be any size then what you really want is a std::vector<vino_t>. A std::vector can be constructed from a initializer list and it also manages the memory for you.


Also note that all your char*'s to string literals are illegal. A string literal has a type of const char[N] and therefore cannot be stored in a char*. You should really consider using a std::string instead. Making all those changes would give you

struct vino_t {
    int tip;
    std::string sorta;
    int godina;
    int cena;
};

struct vinarija_t {
    std::string ime_vinarija;
    std::string lokacija;
    int kolku_vina;
    std::vector<vino_t> lista_vina;
    int zarabotka;
    int nagradi;
};

This also has the added advantage that your classes are now fully move and copy constructable without you having to write a single line of code.

Upvotes: 2

Related Questions