View Full Version : declare array in a class

14th April 2006, 14:22
Hi, I'd like declare float spec[4]; as private member of a class; my problem is that I need do something about this (declare and initialize but inside declare of class seems not possible):

float spec[]={.4f,.4f,.4f,1};

I tried declare

class myClass {
float spec[4];

myClass::myClass() {
but this get an error;
Do I make a for cicle to initialize spec???? Thanks

14th April 2006, 15:14
What error do you get ?

If your variable is static then use :

float myClass::spec[] ={...}

otherwise remove the "[]" in the initialisation.

14th April 2006, 16:47
my variabile is not static; but I have only instance of myClass in my program...and the only one instace of spec.....
I know that I can init an array with '{ }' only when I declare it....

14th April 2006, 22:40
If I declare it static is OK; but I'd like to do it not static:

otherwise remove the "[]" in the initialisation.
if I remove [] from initialization 'spec = {1,2,2,0};' , get an error:

error C2059: syntax error : ']'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
But is it possible to do this? Thanks

17th April 2006, 19:38
In the constructor you will have to declare each element of you array seperately.


of course you could employ a loop to make it easier.

You might then want to develop a constructor that will receive the values in as four arguments.

17th April 2006, 20:25
I hoped there was a way to don't inittialize array element serarately or with for.....