I’m new to programming and I’m trying to create an array of class objects, then initialize each component individually. I manage to declare my class array easily:
Classsystem.h
class RACE {
public:
string name;
string shortdescription;
string description;
int noobjects;
bool objects[N];
int agressiveness;
bool hulls[nohulls];
bool shields[noshields];
bool weapons[noweapons];
bool companies[nocompanies];
double tax;
double bank;
double currency;
int diplomacy[noraces];
double population;
public:
~RACE(){}
RACE() {
name="Default Race";
shortdescription="Default";
description="Default";
noobjects=0;
for(int i=0; i<N;i++) { objects[i]=0; }
agressiveness=0;
for (int i=0; i<nohulls;i++) { hulls[i]=0 ; }
for (int i=0; i<noshields;i++) { shields[i]=0; }
for(int i=0; i<noweapons;i++) { weapons[i]=0; }
for(int i=0; i<nocompanies;i++) { companies[i]=0; }
tax=0;
bank=0;
currency=1;
}
RACE(string iname, int inoobjects, int iagressiveness, double itax, double ibank, double icurrency) {
name=iname;
noobjects=inoobjects;
agressiveness=iagressiveness;
tax=itax;
bank=ibank;
currency=icurrency;
for(int i=0; i<N;i++) { objects[i]=0; }
for(int i=0; i<nohulls;i++) { hulls[i]=1; }
for(int i=0; i<noshields;i++) { shields[i]=1; }
for(int i=0; i<noweapons;i++) { weapons[i]=1; }
for(int i=0; i<nocompanies;i++) { companies[i]=0; }
}
};
races.h:
races[0].name="anything";
main.ccp:
#include "stdafx.h"
#include "Classsystem.h"
RACE races[16];
#include "races.h"
int main(array<System::String ^> ^args)
{
return 0;
}
the errors reads:
races.h(1) : error C2466: cannot allocate an array of constant size 0
races.h(1) : error C2143: syntax error : missing';'before'.'
races.h(1) : error C4430: missing type specifier – int assumed. Note: C++ does not support default-int
races.h(1) : error C2371:'races': redefinition; different basic types
control console.cpp(13) : see declaration of'races'
this still produces the error.
After consulting with my crystal ball, I am going to go for a wild guess to explain the errors:
And the guess is that:
is at namespace level, outside of any function. The language does not allow you to add code at namespace level, and the compiler is getting confused. It is trying to match that against patterns for valid and is considering that
races[0]is a declaration of an array of0elements (error 1) of implicit typeint(C allowed the type specifier to be skipped in a declaration, and would default tointthere –error 3). If that is a declaration, it must be followed by either;or,but the compiler is reading a., so it believes that to belong to the next expression, and that there should be a;before it (error 2). Finally the whole declaration redefines the variableracesto be an array of 0int, while the first definition makes it an array of 16RACE(error 4).