表达式必须具有常量值C ++ [英] expression must have a constant value C++
问题描述
void CountSort( int A [], int B [], int k, int size)
{
const int mx = k;
int C [mx]; // 错误:表达式必须具有常量值!!!
...
}
mx是不变的!这里有什么问题?
[来自问问者的解决方案]
void CountSort( int [], int [] , int , int );
int _tmain( int argc,_TCHAR * argv [])
{
const int size = 12 跨度>;
int A [size] = { 0 , 2 , 3 , 1 , 7 , 9 , 6 , 45 , 12 , 10 , 20 , 15 };
int B [size];
int max = 45 ;
CountSort(A,B,max,size);
// for(int i = 0; i< size;> // cout< ;< A [i]<<;
for ( int i = 1 ; i< size;> cout<< B [i]<< ;
return 0 ;
}
void CountSort( int A [], int B [], int k, int size)
{
int * C = new int [k];
for ( int i = 0 ; i< = k; i ++)
C [i] = < span class =code-digit> 0 ;
for ( int j = 1 ; j C [A [j]] = C [A [j]] + 1 ;
for ( int i = 1 ; i< = k; i ++)
C [i] = C [i] + C [i- 1 ];
for ( int j = size- 1 ; j> 0; j--)
{
B [C [A [j]]] = A [j];
C [A [j]] = C [A [j]] - 1 ;
}
// delete [] C;
}
不,不管怎么说,事实并非如此。我真的很惊讶这个错误并不是那句话。
但是让我们休息吧它下来:mx的值是k,但是compi ler直到运行时才知道k将是什么。这可能是允许的,但是编译器不知道C有多大,这意味着它不能继续。如果你需要一个在编译时未知的大小,你需要一个动态数组。
试试这个:
void CountSort( int A [], int B [], int k, int size)
{
int * C = new int 跨度> [K];
...
delete [] C; // 清理完成后分配的内存
}
数组只能使用编译时常量来初始化,例如
static const int mx = 9 跨度>;
int C [mx];
但你不能为静态常量分配了一个非静态变量。
static const int mx = k; //如果k是函数参数,则无效
最简单的解决方案是使用提供std :: vector的标准模板库模板:
#include < vector >
void CountSort( int A [], int B [], int k, int size)
{
std :: vector< int> C(k)的;
...
}
void CountSort( int A [], int B [] , int k, int size)
{
const int mx = k;
int C [mx]; // 错误:表达式必须具有常量值!!!
...
}
尝试使用new for this
void CountSort(int A [],int B [], int k,int size)
{
const int mx = k;
// int C [mx]; //错误:表达式必须有一个恒定值!!!
int * C = new int [k]; //或新int [mx]无论如何都很好
//你可以把它作为普通的arrray使用
C [somevalue] = someoperation;
...
}
当你在它的时候看看动态内存分配。
void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
int C[mx];//error:expression must have a constant value!!!
...
}
mx is constant! What is wrong here??
[From asker''s "solution"]
void CountSort(int[],int[], int , int);
int _tmain(int argc, _TCHAR* argv[])
{
const int size=12;
int A[size]={0,2,3,1,7,9,6,45,12,10,20,15};
int B[size];
int max=45;
CountSort(A,B,max,size);
//for(int i = 0 ; i<size;> // cout<<A[i]<<" ";
for(int i = 1 ; i<size;> cout<<B[i]<<" ";
return 0;
}
void CountSort(int A[],int B[], int k, int size)
{
int *C = new int[k];
for(int i =0;i<=k;i++)
C[i]=0;
for(int j =1; j C[A[j]] = C[A[j]] + 1 ;
for (int i = 1; i<=k ;i++)
C[i]= C[i] + C[i-1] ;
for (int j = size-1 ;j>0;j--)
{
B[C[A[j]]] = A[j] ;
C[A[j]] = C[A[j]] - 1 ;
}
//delete[] C;
}
No, it doesn''t, not really anyways. I''m a little surprised the error isn''t the line before that honestly.
But let''s break it down: the value of mx is k, but the compiler does not know what k will be until runtime. This might be allowable, but the compiler has no idea how big to make C, which means it can''t continue. If you need a size that''s unknown at compile time, you need a dynamic array.
Try this instead:
void CountSort(int A[],int B[], int k, int size) { int* C = new int[k]; ... delete[] C; // clean up the memory you allocated when you're done with it }
Arrays can only be initialized by using compile time constants like
static const int mx = 9; int C[mx];
But you cannot assigned a non-static variable to a static constant.
static const int mx = k; // Invalid if k is a function parameter
The most simple solution is to use the Standard Template Library which provides the std::vector template:
#include <vector> void CountSort(int A[],int B[], int k, int size) { std::vector<int> C(k); ... }
void CountSort(int A[],int B[], int k, int size) { const int mx=k; int C[mx];//error:expression must have a constant value!!! ... }
try using new for this
void CountSort(int A[],int B[], int k, int size)
{
const int mx=k;
//int C[mx];//error:expression must have a constant value!!!
int* C = new int[k];//or new int[mx] anyway is fine
//You can use it as a normal arrray
C[somevalue]=someoperation;
...
}
and while you''re at it do have a look at dynamic memory allocation.
这篇关于表达式必须具有常量值C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!