cannot allocate an array of constant size 0

Hi there, I'm a bit new to C++, but not programming in general.

I have no idea why this code:

1
2
3
4| float densityList[2];
5| densityList[0]			= 6.0F;
6| densityList[1]			= 13.0F;


Is generating this error:

...\arrays.cpp(5) : error C2466: cannot allocate an array of constant size 0


Line 5 is causing the error, but I'm not trying to create a zero length array, I want to set the value for the first item in the array.

I'm sure I'm missing some basic syntax but from the arrays page here it all seems to be in order.
Last edited on
This works ok for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
    float densityList[2];

    densityList[0]=6.0f;
    densityList[1]=13.0f;

    cout << densityList[0] << endl;
    cout << densityList[1] << endl;

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

Could I see the whole code?
Thats basically it, its just a long list of Arrays (no main function). They all get that error though.
Well the code you posted works, so the problem must be elsewhere. You'll have to post more code if you'll want us to spot the problem.
Are you sure you don't have those line outside a function?
Okay, heres the entire .cpp. I'm working with a non-standard library so things may look odd.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "SkyGame.h"

using namespace C4;

float densityList[2];
densityList[kGas]			= 6.0F;
densityList[kSolid]			= 13.0F;

unsigned long resFoodMinList[2];
unsigned long resFoodMaxList[2];
resFoodMinList[kGas]		= 0;
resFoodMaxList[kGas]		= 20;
resFoodMinList[kSolid]		= 0;
resFoodMaxList[kSolid]		= 100;

unsigned long resFuelMinList[2];
unsigned long resFuelMaxList[2];
resFuelMinList[kGas]		= 0;
resFuelMaxList[kGas]		= 75;
resFuelMinList[kSolid]		= 0;
resFuelMaxList[kSolid]		= 100;

unsigned long resOreMinList[2];
unsigned long resOreMaxList[2];
resOreMinList[kGas]			= 0;
resOreMaxList[kGas]			= 100;
resOreMinList[kSolid]		= 0;
resOreMaxList[kSolid]		= 75;

unsigned long resDustMinList[2];
unsigned long resDustMaxList[2];
resDustMinList[0]			= 0;
resDustMaxList[0]			= 30;
resDustMinList[kSolid]		= 0;
resDustMaxList[kSolid]		= 15;

String<20> planetNameList[24];
planetNameList[0]			= "Alpha";
planetNameList[1]			= "Beta";
planetNameList[2]			= "Gamma";
planetNameList[3]			= "Delta";
planetNameList[4]			= "Epsilon";
planetNameList[5]			= "Zeta";
planetNameList[6]			= "Eta";
planetNameList[7]			= "Theta";
planetNameList[8]			= "Lota";
planetNameList[9]			= "Kappa";
planetNameList[10]			= "Lambda";
planetNameList[11]			= "Mu";
planetNameList[12]			= "Nu";
planetNameList[13]			= "Xi";
planetNameList[14]			= "Omicron";
planetNameList[15]			= "Pi";
planetNameList[16]			= "Rho";
planetNameList[17]			= "Sigma";
planetNameList[18]			= "Tau";
planetNameList[19]			= "Upsilon";
planetNameList[20]			= "Phi";
planetNameList[21]			= "Chi";
planetNameList[22]			= "Psi";
planetNameList[23]			= "Omega";

String<5> planetSuffixList[14];
planetSuffixList[0]			= "I";
planetSuffixList[1]			= "II";
planetSuffixList[2]			= "III";
planetSuffixList[3]			= "IV";
planetSuffixList[4]			= "V";
planetSuffixList[5]			= "VI";
planetSuffixList[6]			= "VII";
planetSuffixList[7]			= "VIII";
planetSuffixList[8]			= "IX";
planetSuffixList[9]			= "X";
planetSuffixList[10]		= "A";
planetSuffixList[11]		= "B";
planetSuffixList[12]		= "C";
planetSuffixList[13]		= "D";
R0mai wrote:
Are you sure you don't have those line outside a function?

R0mai's guess was correct, I see :P

Do it either like this:

1
2
3
float densityList[2]= {6.0F,13.0F};

//... 

or like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
float densityList[2];

//...

void init()
{
    densityList[kGas]= 6.0F;
    densityList[kSolid]= 13.0F;

    //...
}

int main()
{
    init(); //call init here

    //...
}

okay, will try that after work, thanks.
Topic archived. No new replies allowed.