constant array size during declaration ??

N

news.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 ???
 
R

Rolf Magnus

news.hku.hk said:
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;
 
M

marbac

news.hku.hk said:
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;
}
 
J

Jakob Bieling

marbac said:
news.hku.hk said:
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
 
N

news.hku.hk

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
 
R

Rolf Magnus

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

news.hku.hk

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;}
 
J

Jeff Schwab

news.hku.hk said:
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.
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top