mb2357
mb2357

Reputation: 3

Compilation error when trying to initialize vector of structs

I'm trying to initialize vector of structs and I'm getting compilation error.

As far as my understanding goes initializing vector of structs is easy when struct contains straightforward data types like int, float, etc. but what if i have several char arrays inside?

#include <vector>

/// this compiles without any problem:
typedef struct TEST_TYPE_A
{
    int a;
    int b;
    int c;
    int d;
};

std :: vector <TEST_TYPE_A> TEST_A =
{
    {1,2,1,2},
    {4,5,6,4},
    {7,8,8,9},
    {0,1,10,11},
    {3,4,99,200}
};/// so far good, no compilation error


/// this variant fails
typedef struct TEST_TYPE_B
{
    int a;
    int b;
    char txt1[10];
    char txt2[3];
};

std :: vector <TEST_TYPE_B> TEST_B =
{
    {1,2,"1010101111","ABC"},
    {4,5,"1010101111","ABC"},
    {7,8,"1010101111","ABC"},
    {0,1,"1010101111","ABC"},
    {3,4,"1010101111","ABC"}
}; /// i get compilation error here

Compilation error:

error: could not convert '{{1, 2, "1010101111", "ABC"}, {4, 5, "1010101111", "ABC"}, {7, 8, "1010101111", "ABC"}, {0, 1, "1010101111", "ABC"}, {3, 4, "1010101111", "ABC"}}' from '<brace-enclosed initializer list>' to 'std::vector<TEST_TYPE_B>'

I've seen similar problem here with string type instead of char[NUM] array which appeared to be working. I understand that since I'm initializing array it will need some special treatment but I don't know how to do it as simple as possible. I'm not going to pretend I'm educated enough and simply ask whats wrong and how can i fix it? I'm using GCC 5.1 and C++11.

Upvotes: 0

Views: 562

Answers (2)

Eric
Eric

Reputation: 1243

Your txt1 and txt2 members of TEST_TYPE_B are actually arrays. So to initialize them the way you are doing it now you would have to change it to this (just did it for the first TEST_TYPE_B) :

std :: vector <TEST_TYPE_B> TEST_B =
{
    {1,2,{'1','0','1','0','1','0','1','1','1','1'},{'A','B','C'}}
}; 

However, there are better ways than doing this that got already covered in other answers. I mainly added this answer to explain what's exactly wrong with your code. You probably should change your txt1 and txt2 to std::string like in the answer provided by Ayxan.

Upvotes: 0

Aykhan Hagverdili
Aykhan Hagverdili

Reputation: 29965

Use std::string instead of c-style null-terminated arrays. std::string is less error-prone and there is usually no reason to avoid it.

#include <vector>
#include <string>

struct TEST_TYPE_B
{
    int a;
    int b;
    std::string txt1;
    std::string txt2;
};

std::vector <TEST_TYPE_B> TEST_B =
{
    {1, 2, "1010101111", "ABC"},
    {4, 5, "1010101111", "ABC"},
    {7, 8, "1010101111", "ABC"},
    {0, 1, "1010101111", "ABC"},
    {3, 4, "1010101111", "ABC"}
};

Compiles fine

If you want to keep using char[], and arrays are large enough to hold the text, compilers seem to disagree on if the code is valid (clang compiles it, gcc doesn't). But they all agree with this form:

std::vector <TEST_TYPE_B> TEST_B =
{
    TEST_TYPE_B{1, 2, "1010101111", "ABC"},
    TEST_TYPE_B{4, 5, "1010101111", "ABC"},
    TEST_TYPE_B{7, 8, "1010101111", "ABC"},
    TEST_TYPE_B{0, 1, "1010101111", "ABC"},
    TEST_TYPE_B{3, 4, "1010101111", "ABC"}
};

Upvotes: 3

Related Questions