wrote:
> Hi everyone, I'm a long time C coder, who is coding his final year
> project in C++ to run under the MIT click routing system. Being fairly
> new to the OO side of the language, my problem is this:
>
> In C++, how can I define a global array whose size is determined by
> parameters passed to a class?
What do you mean by "parameters passed to a class"? Perhaps you mean
parameters passed to a function?
> The array is of static size once defined, but the parameters of course
> arent read until the config function is called at class startup.
Well, you can use std::vector<> template.
> is
> there a way of prototyping the array with mynewarray[](int blah)[](int
> bleh) or something similar, assuming blah and bleh are the int values
> read from the parameters list in the config function? Both are defined
> in the header, along with the struct type for the array I wish to
> create. I did try just defining it then and there in the header, but
> got an error like this:
I am quite perplexed by "mynewarray[](int blah)[](int bleh)"...What
exactly what you want to have? It looks like an array of arrays of
functions.
>
> myheader.hh:34: error: invalid use of non-static data member
> 'My_Element::bleh'
> myheader.hh:56: error: from this location
> myheader.hh:56: error: array bound is not an integer constant
> myheader.hh:33: error: invalid use of non-static data member
> 'My_Element::blah'
> myheader.hh:56: error: from this location
> myheader.hh:56: error: array bound is not an integer constant
Well, wouldn't it be better if you post the lines in question along with
the error messages?
>
> Like I say, Im pretty new to C++, and there must be a way of just
> prototyping the variables or something, then initialising the array
> properly once the parameters have been passed.
Well yes you can. Just an example:
unsigned int size = get_size_from_config_file();
std::vector<mytype> arr(size);
>
> I did try struct mytype mynewarray[][]; in the header, and mytype
> mynewarray[blah][bleh] in the init function, but it didnt like that
> either.
Well, now it seems like you are trying to make a 2-dimensional array. If
that is what you want, consider a vector of vectors:
std::vector<std::vector<mytype> > arr;
>
> Thanks in advance for any insight, this is driving me mad.
>
> Daniel
>
Regards,
Ben