0

I am trying to allocate a dynamic string array in the following way and I get an error:

struct Test
{
    std::string producer[];
    std::string golden[];
};

Test test1 =
{
        {"producer1", "producer2"} ,
        {"golden1"  , "golden2"}
};

The error i get is that there are too many initializers for std::string[0], but if I get off the array part it works:

struct Test
{
    std::string producer;
    std::string golden;
};

Test test1 =
{
        "producer1" ,
        "golden1"
};

Thanks in advance!

RvdK
  • 19,580
  • 4
  • 64
  • 107
talel
  • 355
  • 2
  • 16
  • `std::string producer[];` You cannot declare an array without saying how many elements is should contain. – Bo Persson Apr 02 '12 at 07:24
  • @Bo you can, but you cannot define it, you have to do it dynamically. – littleadv Apr 02 '12 at 07:25
  • @littleadv Yes, but a non-static member declaration is a definition, and requires the size. And you can't dynamically allocate any thing that has been declared. To dynamically allocate a `string[]`, you'd have to define the object as a `string*`, not a `string[]`. The only places where and actual `string[]` would be legal is as an `extern`, or as a reference parameter to a function (but that's a bit tricky, since you cannot then pass the function an array whose size is known). – James Kanze Apr 02 '12 at 07:52
  • @James, i won't explain it in the comment, but you're wrong. Note that he's defining a type here. – littleadv Apr 02 '12 at 08:00
  • @littleadv Exactly. He's defining a class, and non-static data member declarations in a class are defintions, and require the size, see §9.2/10 ("Non-static data members shall not have incomplete types.") – James Kanze Apr 02 '12 at 08:26

2 Answers2

3

You cannot initialize zero-sized arrays this way, because you have to dynamically allocate the memory. You can only do what you do if you specify the dimensions in the type definition.

See my answer to a similar question here.

Community
  • 1
  • 1
littleadv
  • 20,100
  • 2
  • 36
  • 50
1

You can use C++11 uniform initialization:

struct Test
{
    vector<string> producer;
    vector<string> golden;  
};

Test test1 =
{
    {"producer1", "producer2"},
    {"golden1", "golden2"}
};

The following sample code compiles fine with g++:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Test
{
    vector<string> producer;
    vector<string> golden;  
};


ostream & operator<<(ostream & os, const Test & test)
{
    os << "------------------------" << endl;
    os << "producer={" << endl;
    for (auto& p : test.producer)
    {
        os << '\t' << p << endl;
    }
    os << "}" << endl;

    os << "golden={" << endl;
    for (auto& p : test.golden)
    {
        os << '\t' << p << endl;
    }
    os << "}";
    os << "------------------------" << endl;

    return os;   
}



int main()
{
    Test test1 =
    {
         {"producer1", "producer2"},
         {"golden1", "golden2"}
    };

    cout << test1 << endl;


    return 0;
}