Defining arrays for classes

I know this is a stupid question, but for some reason I can't find the answer to this. How can you define arrays when defining a class object?
Example Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

class Testclass
{
    int test[];
};


int main()
{
    Testclass test={{1},{3}};
    return 0;
}
According to how you initialize the Testclass object, you have two arrays. Perhaps try:
1
2
3
4
   //Split up braces for comments
Testclass test = {  //First brace for initializing test variables
                               {1,3} //Second brace pair for test[] array
                          };
For some reason that won't work.
Well, in this case, I would create a constructor like Testclass(initializer_list<int>) (you need <initializer_list> library for this) to handle initialization of the array. Or, if you know the exact number of values the array is going to have, then you can make a constructor like Testclass(int a, int b, int c,...).
You may not define incomplete type fir non-static data member. In your case you shall specify the size of the array. Also if you want to initialize it the way you are using the array shall be a public data member

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

class Testclass
{
piblic:
    enum { SIZE = 10 };
    int test[SIZE];
};


int main()
{
    Testclass test = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 } };
    return 0;
}
Last edited on
Thank you for taking charge on this vlad. I feel dumb for not realizing that.
Topic archived. No new replies allowed.