Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I tried many things but didn't know how to fix it.
 
int size;
cout<<" Enter The Amount of Rooms : ";
cin >> size;
 
const int x=size;
Point *Room[x]; //expression must have a constant value;

Posted 13-Feb-13 0:09am
Edited 13-Feb-13 0:34am
v2
Comments
Matej Hlatky at 13-Feb-13 6:25am
   
I think this code won't compile in any version of C#.
missak boyajian at 13-Feb-13 6:35am
   
Ooops I am working C++. I put c# by mistake.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You cannot do that. You should dynamically allocate memory instead.
Point **Room = new Point*[size]; 
 
BTW don't forget to delete it then.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The expression must be constant at compile time. Your code uses a run-time constant. A compile time constant would look like this:
const int x = 5;
Point *Room[x]; // OK here
To create an array at runtime, use the new operator:
Point *Room = new Point[size];
// Use Room here
// ...
// Delete it when finished
delete [] Room;
 
[EDIT]
Noted that Carlo has edited his solution to use an array of pointers to Point to use the same types as in the question. I will left my example unchanged, because it may be the intention to use this kind of array. If not, Carlo's answer should be used.
[/EDIT]
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 480
1 Tadit Dash 305
2 Sergey Alexandrovich Kryukov 250
3 Peter Leow 190
4 OriginalGriff 163
0 Sergey Alexandrovich Kryukov 9,435
1 OriginalGriff 5,498
2 Peter Leow 4,230
3 Maciej Los 3,540
4 Abhinav S 3,353


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid