谁见过这个错误:pegc.cpp:1140: error: crosses initialization of `JEnc m' ?

积木 2004-08-16 03:51:43
pegc.cpp:1140: error: crosses initialization of `JEnc m'
jpegc.cpp:1130: error: crosses initialization of `char outFile[11]'

或者能提供相关信息的,玉门死我了
...全文
2064 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
RookieStar 2004-08-16
  • 打赏
  • 举报
回复
看这个:
switch (expr)
{
int i = 4;
f(i);
case 0:
i = 17;
/* falls through into default code */
default:
printf("%d\n", i);
}

这里的i不会得到初始化(4),如果expr是非零值,printf出来的是个不确定的值。
sandrowjw 2004-08-16
  • 打赏
  • 举报
回复
厄,没有看那段英文,原来楼主已经搞定了。

似乎case后面的label和普通的label是同样处理的,而C++是不允许跳过当前静态域中的初始化代码的。
sandrowjw 2004-08-16
  • 打赏
  • 举报
回复
用{}把两段代码包起来哪?
积木 2004-08-16
  • 打赏
  • 举报
回复
是啊
switch 不过是一种采用表方式挑砖代码的语句体,让他做这个是难了点
RookieStar 2004-08-16
  • 打赏
  • 举报
回复
我觉得这是在分支语句里定义并初始化变量所致。

switch (a)
{
case 1:int i=5;break;
case 2:int j=6;break;
};

如果换成纯声明即int i;和int j;就不会有问题了。

写这个分支的目的是为了在不同的情况下作不同的初始化,但编译器在编译时却确定不了到底是对哪个分支初始化。肯定不可能是两者之一,因为它没这个能力在编译期判断;都作初始化,不可能,那就违背了这个分支的本意;都不初始化,不可能,我们都知道初始化是在编译期就要搞定的。

你的问题不是出在声明,而是这个初始化Jpeg j; j这个对象通过默认构造函数初始化了,导致编译期的错误。

以上是我的看法。
积木 2004-08-16
  • 打赏
  • 举报
回复
"The compiler objects to my declaring a variable in one of the branches of a case statement. Earlier versions used to accept this code. Why?"

The draft standard does not allow a goto or a jump to a case label to skip over an initialization of a variable or a class object. For example:


switch ( i )
{
case 1:
Object obj(0);
...
break;
case 2:
...
break;
}

The reason is that obj is also in scope in the rest of the switch statement.

As of version 2.7.0, the compiler will object that the jump to the second case level crosses the initialization of obj. Older compiler versions would object only if class Object has a destructor. In either case, the solution is to add a set of curly braces around the case branch:


case 1:
{
Object obj(0);
...
break;
}

积木 2004-08-16
  • 打赏
  • 举报
回复
既然已经写了,就自问自答吧
这个错误原来出自我写得这样一段代码
switch(i)
{
case 1:
string s;
break;
case 2:
Jpeg j;
break;
}
编译器对这两个声明报错。只要将这两个变量声明放在switch外面就可以了
但是why ?
ehhl 2004-08-16
  • 打赏
  • 举报
回复
mark

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧