global array defined by parameters passed. protoyping in header?

D

danbraund

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?
The array is of static size once defined, but the parameters of course
arent read until the config function is called at class startup. 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:

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

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.

I did try struct mytype mynewarray[][]; in the header, and mytype
mynewarray[blah][bleh] in the init function, but it didnt like that
either.

Thanks in advance for any insight, this is driving me mad.

Daniel
 
R

Rolf Magnus

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 "passed to a class"? Does it have to be a raw array?
The array is of static size once defined, but the parameters of course
arent read until the config function is called at class startup. 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?

No. Arrays always have a fixed size. However, you can dynamically allocate
one or you can use a vector.

std::vector<mytype> myvector;

Then later, you can simply append objects with something like:

myvector.push_back(mynewobject);
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:

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

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.

I did try struct mytype mynewarray[][]; in the header, and mytype
mynewarray[blah][bleh] in the init function, but it didnt like that
either.

An array always must have a size, so the first one is not possible. Also,
the size of static arrays must be a compile-time constant.
 
B

benben

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.

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 said:
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 said:
Thanks in advance for any insight, this is driving me mad.

Daniel

Regards,
Ben
 
D

danbraund

the vectors seem to be the way forward. thanks, and sorry for my
muddledness, I havent slept in days.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top