const as array size

A

asit

We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}
}

Why the above code shows an error ??

How can we overcome it ??
 
J

Juha Nieminen

asit said:
We know that array size can be a constant integer.

No. The array size must be resolvable at compile time. There's a
difference.
Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}
}

Why the above code shows an error ??

Because the value of 'size' cannot be resolved at compile time. The
compiler cannot know how big 'arr', and consequently, how big 'myc' is.
It cannot instantiate it because it can't know how big it should be.
How can we overcome it ??

If 'arr' has always the same size, let's say for example 123, do it
like this:

class myc
{
static const int size = 123;
int arr[size];
...
};

If what you want is a variable-sized array, you can't use that. You'll
have to use:

class myc
{
std::vector<int> arr;

public:
myc(int x): arr(x) {}
}
 
K

Kyle

asit napisał(a):
We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}
}

Why the above code shows an error ??

because array size needs to be a compile time constant integer
How can we overcome it ??

by using std::vector<int>
 
Í

Íõ¾ý³¼

We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}

}

Why the above code shows an error ??

How can we overcome it ??
template<int SIZE>
class myc
{
int arr[SIZE];
//......
}
void main()
{
myc<6> c1;// arr size wil be 6;
myc<8> c2;// arr size will be 8;
}
 
A

acehreli

We know that array size can be a constant integer.
Consider the following case ??
calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}

Why the above code shows an error ??
How can we overcome it ??

template<int SIZE>
class myc
{
int arr[SIZE];
//......}

void main()

That's not C++! You might have meant to write

int main()
{
myc<6> c1;// arr size wil be 6;
myc<8> c2;// arr size will be 8;

Not necessarily 6 or 8, unless it's a legal C++ program.

Ali
 
A

Andrey Tarasevich

asit said:
We know that array size can be a constant integer.

Size specifier in the array declaration must be an Integral Constant Expression.
Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}
}

Why the above code shows an error ??

Because in your case the size is not an Integral Constant Expression.
How can we overcome it ??

There are many different ways. For example

#include <vector>

class myc
{
std::vector<int> arr;
public:
myc(int x) : arr(x) {}
};
 
M

Michael.Boehnisch

We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}

}

Why the above code shows an error ??

How can we overcome it ??

You cannot assign to size in the constructor by size(x) because it is
const - You cannot eat the fish and have it on the table at the same
time.
I recommend to just use an STL container for your problem, e.g.
vector<T>:

class myc {

std::vector<int> arr;

public:

myc( const int x ) : arr( x ) {}

}

best,

mu.
 
T

Thomas J. Gritzan

We know that array size can be a constant integer.

Consider the following case ??

calss myc
{
const int size;
int arr[size];
public:
myc(int x):size(x){}

}

Why the above code shows an error ??

How can we overcome it ??

You cannot assign to size in the constructor by size(x) because it is
const

Thats not an assignment, its initialization, and is the only way to set a
value to a const member in a class.
The error is in the array size specifier that must be a compile time constant.
You cannot eat the fish and have it on the table at the same
time.

I do that regularly.
I recommend to just use an STL container for your problem, e.g.
vector<T>:

class myc {

std::vector<int> arr;

public:

myc( const int x ) : arr( x ) {}

}

Good suggestion, however.
 

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

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,504
Latest member
SherryFerr

Latest Threads

Top