📄 subject_26769.htm
字号:
<p>
序号:26769 发表者:brown 发表日期:2003-01-05 11:10:45
<br>主题:vc++ local variable 的生命週期
<br>内容:各位前輩,我的源碼如下.<BR>用 c++ builder compile 正常,用 vc++ 卻有 error<BR>error C2374: 'i' : redefinition; multiple initialization.<BR>在 c++, loop 1 的 int i 在離開 loop 1 時應就已結束.<BR>所以 loop 2 的 int i 不應該會 error C2374: 'i' : redefinition; multiple initialization<BR>請問是我的 vc++ 還要做甚麼設定嗎?<BR><BR><BR><BR>#include "stdafx.h"<BR>#include <iostream><BR>using namespace std;<BR>int ia[10] = {51,23,7,88,41,98,12,103,37,6};<BR>int main(int argc, char* argv[])<BR>{<BR> // loop 1 <BR> for (int i = 0; i < 5; i++)<BR> cout << ia[i];<BR> cout<< endl;<BR> // loop 2<BR> for (int i = 0; i < 5; i++)<BR> cout << ia[i];<BR> return 0;<BR>}<BR>
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:晚风也渡 回复日期:2003-01-05 11:45:45
<br>内容:#include "stdafx.h"<BR>#include <iostream><BR>using namespace std;<BR>int ia[10] = {51,23,7,88,41,98,12,103,37,6};<BR>int main(int argc, char* argv[])<BR>{<BR> // loop 1 <BR> for (int i = 0; i < 5; i++)<BR> cout << ia[i];<BR> cout<< endl;<BR> // loop 2<BR> for (int j = 0; j < 5; i++)<BR> cout << ia[i];<BR> return 0;<BR>}<BR>在loop1结束后,i的值为5,并没有结束。<BR>如果你想要作另一个循环的话,就另外设一个变量吧。如上所示
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:ThinMonkey 回复日期:2003-01-05 12:04:28
<br>内容:“在 c++, loop 1 的 int i 在離開 loop 1 時應就已結束.”<BR>愚认为i==5,而你在loop 2中又有int i = 0,故有redefinition; multiple initialization的错误,只将loop 2中的int去掉即可。<BR><BR>哟,咱晚了一步:)<BR>2003-1-5 12:05:53
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -