“错误C2057:期望的常量表达式”,“错误C2466:不能分配常数大小为0的数组”。为什么我的简单程序不起作用? [英] "error C2057: expected constant expression", "error C2466: cannot allocate an array of constant size 0". Why doesn't my simple program work???

查看:114
本文介绍了“错误C2057:期望的常量表达式”,“错误C2466:不能分配常数大小为0的数组”。为什么我的简单程序不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:

我有以下简单程序:


#include< iostream>

using namespace std;

int main(int argc,char * argv []){


const double L = 1.234;

const int T = static_cast< const int>(L);

int arr [T];

返回0;

}

但我收到标题中显示的错误消息。为什么我的计划不是
工作?谢谢你的帮助!

解决方案

在2007-01-20 17:08, hn ******** @ gmail.com 写道:


嗨:

我有以下简单程序:


#include< iostream>

使用命名空间std;

int main(int argc,char * argv []){


const double L = 1.234;

const int T = static_cast< const int>(L );

int arr [T];

返回0;

}


但是我获取标题中显示的错误消息。为什么我的计划不是
工作?感谢帮助!



因为T在编译时不是常量,所以需要为

编译器知道要分配多少空间堆栈。

当然,根据标准不是正确的解释,但它很好地描述了发生了什么。考虑以下代码

哪个T也是常数,但每次运行时都要小心:


#include< iostream>


int main()

{

双L;

std :: cin> L;

const int T = static_cast< const int>(L);

返回0;

}


-

Erik Wikstr?m


hn ******** @ gmail.com 写道:


嗨:

我有以下简单程序:


#include< iostream>

使用命名空间std;

int main(int argc,char * argv [ ]){

const double L = 1.234;

const int T = static_cast< const int>(L);

int arr [T ];

返回0;

}


为什么我的程序不起作用?



好​​问题。代码编译好我的g ++编译器,但不适用于
http:// www。 comeaucomputing.com/tryitout 我有一些关于

comeau的输出的问题:


" ComeauTest.c" ,第6行:警告:类型限定符在强制转换类型上没有意义

const int T = static_cast< const int>(L);

^


这是什么?在上面的上下文中不是'const''吗?


" ComeauTest.c",第7行:错误:常数值未知

int arr [T];

^



为什么会这样?


Erik Wikstr?m写道:


2007-01-20 17:08, hn ******** @ gmail.com 写道:


>嗨:
我有以下简单程序:

#include< iostream>
使用命名空间std;
int main(int argc,char * argv []){

const double L = 1.234;
const int T = static_cast< const int>(L);
int arr [T];
返回0;
}

但我收到标题中显示的错误消息。为什么我的计划不起作用?感谢帮助!



因为T在编译时不是常数,



嗯?那是怎么回事?


它需要为

编译器知道在堆栈上分配多少空间。

当然,根据标准不是正确的解释,但它很好地描述了发生了什么。考虑以下代码

哪个T也是常数,但每次运行时都要小心:


#include< iostream>


int main()

{

双L;



你在上面的行中作弊。 OP有:


const double L = 1.234;

^^^^^


std :: cin> L;



使用OP的行,这将是未定义的行为。


const int T = static_cast< ; const int>(L);

返回0;

}



最佳


Kai-Uwe Bux


Hi:
I have the following simple program:

#include<iostream>
using namespace std;
int main(int argc, char* argv[]){

const double L = 1.234;
const int T = static_cast<const int>(L);
int arr[T];
return 0;
}

But I get the error message shown in title. Why doesn''t my program
work? Thanks for help!

解决方案

On 2007-01-20 17:08, hn********@gmail.com wrote:

Hi:
I have the following simple program:

#include<iostream>
using namespace std;
int main(int argc, char* argv[]){

const double L = 1.234;
const int T = static_cast<const int>(L);
int arr[T];
return 0;
}

But I get the error message shown in title. Why doesn''t my program
work? Thanks for help!

Because T is not constant at compile-time, which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:

#include<iostream>

int main()
{
double L;
std::cin >L;
const int T = static_cast<const int>(L);
return 0;
}

--
Erik Wikstr?m


hn********@gmail.com wrote:

Hi:
I have the following simple program:

#include<iostream>
using namespace std;
int main(int argc, char* argv[]) {
const double L = 1.234;
const int T = static_cast<const int>(L);
int arr[T];
return 0;
}

Why doesn''t my program work?

Good question. The code compiled fine for my g++ compiler, but not for
http://www.comeaucomputing.com/tryitout I have some questions about
comeau''s output:

"ComeauTest.c", line 6: warning: type qualifier is meaningless on cast type
const int T = static_cast<const int>(L);
^

What is this? Isn''t the ''const'' required in the above context?

"ComeauTest.c", line 7: error: constant value is not known
int arr[T];
^

Why is that?


Erik Wikstr?m wrote:

On 2007-01-20 17:08, hn********@gmail.com wrote:

>Hi:
I have the following simple program:

#include<iostream>
using namespace std;
int main(int argc, char* argv[]){

const double L = 1.234;
const int T = static_cast<const int>(L);
int arr[T];
return 0;
}

But I get the error message shown in title. Why doesn''t my program
work? Thanks for help!


Because T is not constant at compile-time,

Huh? How is that?

which it needs to be for the
compiler to know how much space to allocate on the stack. That, of
course, is not the correct explanation according to the standard but it
quite nicely describes what is going on. Consider the following code in
which T is also constant but can wary with every run of the application:

#include<iostream>

int main()
{
double L;

You are cheating in the line above. The OP had:

const double L = 1.234;
^^^^^

std::cin >L;

With the line of the OP, this would be undefined behavior.

const int T = static_cast<const int>(L);
return 0;
}


Best

Kai-Uwe Bux


这篇关于“错误C2057:期望的常量表达式”,“错误C2466:不能分配常数大小为0的数组”。为什么我的简单程序不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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