Re: constant array size during declaration ??

From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 04/25/04


Date: Sun, 25 Apr 2004 09:22:54 -0400

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

Those dots are a syntax error, and you've forgotten a semicolon.

> int main(){
>
> int xxx = 888;
> const int integer = xxx; // why this will generate error ??

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.

> abc obj[integer];
>
> return 0;}

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.
   }
}



Relevant Pages

  • Re: Join strings and assign value
    ... Any suggestions besides the advice? ... 2)Then I do the following to replicate 1 ... A possible cause of this error is that you forgot to initialize the ... In your case I would suggest you take a closer look at structs. ...
    (comp.soft-sys.matlab)
  • Group Policy Object Editor snap-in failed to initialize
    ... Can I download this file for Microsoft and overwrite to ... Please advice, ... >Please advise how to initialize the Group Policy Object ...
    (microsoft.public.windows.group_policy)
  • Re: Float as Infinity
    ... I need to initialize 2 floats (one with minus infinity and one with ... The mistake is made so easily and so frequently that it almost qualifies as ...
    (comp.lang.c)
  • Re: Initializing Variables
    ... Basically, I initialize pretty much every variable as I declare it, just to ... will cause compile-time errors if an unitialized variable is accessed. ... the Java Tutorial is aimed at Java beginners so this particular advice ... initialize no fields inline and assign values to all of them ...
    (comp.lang.java.programmer)
  • Re: Notification Icon
    ... her a links to some examples how to add Notifications in WM 5.0 I guess ... that the only mistake you did that you didn't initialize the " HWND ... handler that will receive the events ...
    (microsoft.public.windowsce.embedded)