Re: constant array size during declaration ??
From: Jeff Schwab (jeffplus_at_comcast.net)
Date: 04/25/04
- Next message: William: "C++ plugin framework"
- Previous message: Marc Le Roy: ""Ravenscar-like" profile for C/C++"
- In reply to: news.hku.hk: "Re: constant array size during declaration ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
}
}
- Next message: William: "C++ plugin framework"
- Previous message: Marc Le Roy: ""Ravenscar-like" profile for C/C++"
- In reply to: news.hku.hk: "Re: constant array size during declaration ??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|