这段代码为何在vc下通不过?在cb下可以啊。

annkie 2003-11-10 09:54:38
Functions.cpp
\cpp\helloworld\functions.cpp(84) : error C2374: 'i' : redefinition; multiple initialization
cpp\helloworld\functions.cpp(82) : see declaration of 'i'
cpp\helloworld\functions.cpp(84) : error C2374: 'j' : redefinition; multiple initialization
cpp\helloworld\functions.cpp(80) : see declaration of 'j'
Error executing cl.exe.
Creating browse info file...

unsigned int const m = 10;
void BucketSort(unsigned int a[],unsigned int n)
{
int buckets[m];
for(unsigned int j=0;j<m;++j)
buckets[j]=0;
for(unsigned int i=0;i<n;++i)
++buckets[a[i]];
for(unsigned int i=0,j=0;j<m;++j)//这里通不过!
{
for(unsigned int k=buckets[j];k>0;--k)
a[i++]=j;
}
}

unsigned int a[]={2,3,5,3,3,6,9,8};
BucketSort(a,sizeof(a)/sizeof(a[0]));
for(unsigned int i=0;i<sizeof(a)/sizeof(a[0]);i++)
cout << a[i] << endl;
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
annkie 2003-11-10
  • 打赏
  • 举报
回复
谢谢楼上两位,偶明白了。
annkie 2003-11-10
  • 打赏
  • 举报
回复
天,真的是这样
for(i=0,j=0;j<m;++j)
这算什么? 没有声明类型的局部变量? omygod 这样怎么可以通过编译呢?
smalltalk 2003-11-10
  • 打赏
  • 举报
回复
是这样的,VC的编译器在局部变量问题上不太符合C++的标准,而BC比较符合。

上面的代码:
for(unsigned int j=0;j<m;++j)
buckets[j]=0;

对于VC,这样的代码实际上是这样展开的:
unsigned int j;
for(j=0; j<m;++j)
buckets[j]=0;

知道这点,就会明白VC为什么会报redefinition; multiple initialization错误了。
所以我们在写商用程序时不同编译器的差异也必须考虑,这是代码移植性方面的问题。

darcymei 2003-11-10
  • 打赏
  • 举报
回复
代码没问题,vc对各个域内名字空间支持的不是很好,把unsigned int i=0,j=0的unsigned int去掉

69,373

社区成员

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

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