How to initialize an array


#1

Howdy

I'm trying to initialize and array in my header file, something like

int divs[5]{ 2, 3, 4, 6, 8 };

This compiles fine on mac, but not so much on windows.

I'd like to use the Juce array stuff, but I cannot figure out the exact syntax, or if I do some of this in the header and some in the constructor.

So far I've got Array<int> divs but I can't get much further than that.

Could anyone point me in the right direction?

Thanks


#2

What you wrote there is C++11. Try 

int divs[] = { 1, 2, 3 4 };


#3

int divs[] = {1,2,3,4,5} doesnt work at all ('incomplete type not allowed), and I should have mentioned I tried int divs[5] = {1,2,3,4,5} first, which will not compile ('cannot specify explicit initliaser for arrays').


#4

No.. what I wrote is definitely valid syntax.


#5

Ok if I do

int divs[5];

in the header and then

divs[5] = (2, 3, 4, 6, 8);

in the constructor function, all is well.


#6

It looks right to me, but I get:

'incomplete type is not allowed' as an error while editing (VS2013), and

'... has an illegal zero-sized array'

on compile

 


#7

Still interested how I would do this with the Juce array class.

Thanks


#8
const int x[] = { 1, 2, 3 };

Array<int> foo (x, numElementsInArray (x));

#9

Thanks - but I still get 'incomplete type not allowed' on that const int x[] = {1,2,3};

That is in the .h file, in the .cpp it works fine.

 

...and as I'm sure you know, divs[5] = (2, 3, 4, 6, 8); doesn't actually work at all. I just got excited cause it compiled.