global array defined by parameters passed. protoyping in header?

Discussion in 'C++' started by danbraund@gmail.com, Apr 8, 2006.

  1. Guest

    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
     
    , Apr 8, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    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.
     
    Rolf Magnus, Apr 8, 2006
    #2
    1. Advertising

  3. benben Guest

    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
     
    benben, Apr 8, 2006
    #3
  4. Guest

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

    Dan
     
    , Apr 8, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dee
    Replies:
    9
    Views:
    515
    Joseph Byrns
    Apr 15, 2005
  2. Anand
    Replies:
    2
    Views:
    906
    Anand
    Sep 11, 2003
  3. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  4. Replies:
    1
    Views:
    506
    Sion Arrowsmith
    Jul 10, 2008
  5. Why Tea

    Array passed as function parameters

    Why Tea, Aug 16, 2012, in forum: C Programming
    Replies:
    21
    Views:
    776
    ais523
    Aug 26, 2012
Loading...

Share This Page