表达式必须具有常量值 [英] Expression must have a constant value

查看:418
本文介绍了表达式必须具有常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多东西,但不知道如何修复它。



  int 大小; 
cout<<输入 金额 客房 ;
cin >>尺寸;

const int x = size;
Point * Room [x]; // 表达式必须具有常量值;

解决方案

你不能这样做。你应该动态地分配内存。

 Point ** Room =  new 点* [大小]; 





BTW别忘了删除然后。


< blockquote>表达式在编译时必须是常量。您的代码使用运行时常量。编译时常量如下所示:

  const   int  x =  5 ; 
积分*房间[x]; // 好在这里



创建一个数组在运行时,使用 new 运算符:

 Point * Room = 点[size]; 
// 在此使用空间
// ...
// 完成后删除
delete [] Room;







注意到Carlo编辑了他的解决方案,使用指向 Point 的指针数组来使用与问题中的类型相同。我将保持我的例子不变,因为它可能是使用这种数组的意图。如果没有,应该使用Carlo的回答。

[/ EDIT]


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;

解决方案

You cannot do that. You should dynamically allocate memory instead.

Point **Room = new Point*[size]; 



BTW don''t forget to delete it then.


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]


这篇关于表达式必须具有常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆