Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > global array defined by parameters passed. protoyping in header?

Reply
Thread Tools

global array defined by parameters passed. protoyping in header?

 
 
danbraund@gmail.com
Guest
Posts: n/a
 
      04-08-2006
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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      04-08-2006
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 "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.

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


 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      04-08-2006
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
 
Reply With Quote
 
danbraund@gmail.com
Guest
Posts: n/a
 
      04-08-2006
the vectors seem to be the way forward. thanks, and sorry for my
muddledness, I havent slept in days.

Dan

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
using global.asax.vb for defining global parameters OK ASP .Net 5 11-26-2007 12:59 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



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