constant array size during declaration ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.hku.hk

    constant array size during declaration ??

    suppose i have a class abc, and i have an important value stored as

    int integer = 888;

    when i want to declare an array of objects:

    abc obj[integer]; // error said non-constant....exp ect constant array
    size....and the like

    What can i do if i really want 888 as the array size ???


  • Rolf Magnus

    #2
    Re: constant array size during declaration ??

    news.hku.hk wrote:
    [color=blue]
    > suppose i have a class abc, and i have an important value stored as
    >
    > int integer = 888;
    >
    > when i want to declare an array of objects:
    >
    > abc obj[integer]; // error said non-constant....exp ect constant
    > array size....and the like
    >
    > What can i do if i really want 888 as the array size ???[/color]

    Make the integer const:

    const int integer = 888;

    Comment

    • marbac

      #3
      Re: constant array size during declaration ??

      news.hku.hk schrieb:[color=blue]
      > suppose i have a class abc, and i have an important value stored as
      >
      > int integer = 888;
      >
      > when i want to declare an array of objects:
      >
      > abc obj[integer]; // error said non-constant....exp ect constant array
      > size....and the like
      >
      > What can i do if i really want 888 as the array size ???
      >
      >[/color]


      I tried it, and there seems to be no problem to declare an array of objects.


      #include <iostream>

      class abc {
      public:
      void set_value (int x) { this->value=x; };
      int get_value () { return this->value; };
      private:
      int value;
      };

      int main () {
      int integer = 888;
      abc test [integer];
      test[444].set_value (5);
      std::cout << test[444].get_value() << std::endl;
      return 0;
      }

      Comment

      • Jakob Bieling

        #4
        Re: constant array size during declaration ??

        "marbac" <marbac@chello. at> wrote in message
        news:OiMic.5462 62$Or1.431541@n ews.chello.at.. .[color=blue]
        > news.hku.hk schrieb:[color=green]
        > > suppose i have a class abc, and i have an important value stored as
        > >
        > > int integer = 888;
        > >
        > > when i want to declare an array of objects:
        > >
        > > abc obj[integer]; // error said non-constant....exp ect constant array
        > > size....and the like
        > >
        > > What can i do if i really want 888 as the array size ???[/color][/color]
        [color=blue]
        > I tried it, and there seems to be no problem to declare an array of[/color]
        objects.

        It works because you are using a non-Standard extension of your
        compiler.

        hth
        --
        jb

        (replace y with x if you want to reply by e-mail)


        Comment

        • news.hku.hk

          #5
          Re: constant array size during declaration ??

          i've tried to write:

          const int integer =888;
          abc obj[integer];

          but there are still 3 errors:
          expected constant expression
          cannot allocate an array of constant size 0
          'abc' : unknown size

          what should i do? Thanks

          "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message
          news:408b8c0a@n ewsgate.hku.hk. ..[color=blue]
          > suppose i have a class abc, and i have an important value stored as
          >
          > int integer = 888;
          >
          > when i want to declare an array of objects:
          >
          > abc obj[integer]; // error said non-constant....exp ect constant array
          > size....and the like
          >
          > What can i do if i really want 888 as the array size ???
          >
          >[/color]


          Comment

          • Rolf Magnus

            #6
            Re: constant array size during declaration ??

            news.hku.hk wrote:
            ^^^^^^^^^^^
            You are supposed to write your name in the From: field, not the one of
            your news server. And please don't top-post/full-quote.
            [color=blue]
            > i've tried to write:
            >
            > const int integer =888;
            > abc obj[integer];
            > but there are still 3 errors:
            > expected constant expression
            > cannot allocate an array of constant size 0
            > 'abc' : unknown size
            >
            > what should i do? Thanks[/color]

            Show us a complete program that produces that error. The following
            program doesn't produce any error message here:

            class abc{};
            int main(){}

            const int integer =888;
            abc obj[integer];

            Comment

            • news.hku.hk

              #7
              Re: constant array size during declaration ??

              thanks for your advice.

              in fact, i discover what's the mistake now,
              my program should look like this:

              class abc{.......}
              int main(){

              int xxx = 888;
              const int integer = xxx; // why this will generate error ??
              abc obj[integer];

              return 0;}


              Comment

              • Jeff Schwab

                #8
                Re: constant array size during declaration ??

                news.hku.hk wrote:[color=blue]
                > thanks for your advice.
                >
                > in fact, i discover what's the mistake now,
                > my program should look like this:
                >
                > class abc{.......}[/color]

                Those dots are a syntax error, and you've forgotten a semicolon.
                [color=blue]
                > int main(){
                >
                > int xxx = 888;
                > const int integer = xxx; // why this will generate error ??[/color]

                Because you've initialized the "constant" with a non-const value.
                Initialize the constant with another constant, or a literal value. If
                you feel there is no way to do this within your program, e.g. because
                the value of the variable is not known at compile time, consider using
                std::vector instead of a raw array.
                [color=blue]
                > abc obj[integer];
                >
                > return 0;}[/color]

                struct T { };

                #include <cstddef>
                #include <vector>

                int main ( )
                {
                {
                std::size_t const size = 3;

                T objects[ size ]; // Size must be a compile-time constant.
                }

                {
                std::size_t size = 3;

                std::vector< T > vector( size ); // Size can be determined at run time.
                }
                }

                Comment

                Working...