|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ?? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
asit wrote:
> 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) {} } |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 2ÔÂ23ÈÕ, ÏÂÎç11ʱ21·Ö, asit <lipu...@gmail.com> wrote:
> 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; } |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Feb 23, 9:57 am, "Íõ¾ý³¼" <WangJunc...@gmail.com> wrote:
> On 2ÔÂ23ÈÕ, ÏÂÎç11ʱ21·Ö, asit <lipu...@gmail.com>wrote: > > > 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
asit wrote:
> 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) {} }; -- Best regards, Andrey Tarasevich |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On 23 Feb., 16:21, asit <lipu...@gmail.com> wrote:
> 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. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Michael.Boehnisch@gmail.com schrieb:
> On 23 Feb., 16:21, asit <lipu...@gmail.com> wrote: >> 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. -- Thomas http://www.netmeister.org/news/learn2quote.html post tenebras lux. post fenestras tux. |
|
![]() |
| Outils de la discussion | |
|
|