469,133 Members | 1,125 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

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....expect constant array
size....and the like

What can i do if i really want 888 as the array size ???
Jul 22 '05 #1
7 2208
news.hku.hk wrote:
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....expect constant
array size....and the like

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


Make the integer const:

const int integer = 888;

Jul 22 '05 #2
news.hku.hk schrieb:
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....expect constant array
size....and the like

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

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;
}
Jul 22 '05 #3
"marbac" <ma****@chello.at> wrote in message
news:Oi*********************@news.chello.at...
news.hku.hk schrieb:
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....expect constant array
size....and the like

What can i do if i really want 888 as the array size ???
I tried it, and there seems to be no problem to declare an array of

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)
Jul 22 '05 #4
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.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.hku.hk...
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....expect constant array
size....and the like

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

Jul 22 '05 #5
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.
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


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];

Jul 22 '05 #6
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;}
Jul 22 '05 #7
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.
}
}
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by pvinodhkumar | last post: by
8 posts views Thread by Tony Johansson | last post: by
10 posts views Thread by Ricardo Gibert | last post: by
11 posts views Thread by Mantorok Redgormor | last post: by
15 posts views Thread by skibud2 | last post: by
26 posts views Thread by aruna.mysore | last post: by
4 posts views Thread by Peskov Dmitry | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.