久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在循環中重新聲明 for 循環變量時出錯

Error redeclaring a for loop variable within the loop(在循環中重新聲明 for 循環變量時出錯)
本文介紹了在循環中重新聲明 for 循環變量時出錯的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

考慮這個 C 程序片段:

for(int i = 0; i <5; i++){國際我= 10;//<- 注意局部變量printf("%d", i);}

它編譯沒有任何錯誤,并且在執行時給出以下輸出:

1010101010

但是如果我用 C++ 寫一個類似的循環:

for(int i = 0; i <5; i++){國際我= 10;std::cout <<一世;}

編譯失敗并出現此錯誤:

prog.cc:7:13: 錯誤:'int i' 的重新聲明國際我= 10;^prog.cc:5:13: 注意:'int i' 之前在這里聲明過for(int i = 0; i <5; i++)^

為什么會這樣?

解決方案

這是因為 C 和 C++ 語言對于在嵌套在 for 循環中的范圍內重新聲明變量有不同的規則:

  • C++i 放在循環體的作用域內,所以第二個int i = 10 是重聲明,禁止莉>
  • C 允許在 for 循環內的范圍內重新聲明;最里面的變量獲勝"

這是一個運行C程序的演示,以及一個C++ 程序無法編譯.

在正文中打開嵌套范圍修復了編譯錯誤(demo):

for (int i =0 ; i != 5 ; i++) {{國際我= 10;cout<<我<<結束;}}

現在for頭中的iint i = 10在不同的范圍內,所以程序可以運行了.>

Consider this snippet of a C program:

for(int i = 0; i < 5; i++)
{
    int i = 10;  // <- Note the local variable

    printf("%d", i); 
}    

It compiles without any error and, when executed, it gives the following output:

1010101010

But if I write a similar loop in C++:

for(int i = 0; i < 5; i++)
{
     int i = 10;

     std::cout << i; 
}

The compilation fails with this error:

prog.cc:7:13: error: redeclaration of 'int i'  
     int i = 10;  
         ^  
prog.cc:5:13: note: 'int i' previously declared here  
     for(int i = 0; i < 5; i++)  
             ^   

Why is this happening?

解決方案

This is because C and C++ languages have different rules about re-declaring variables in a scope nested in a for loop:

  • C++ puts i in the scope of loop's body, so the second int i = 10 is a redeclaration, which is prohibited
  • C allows redeclaration in a scope within a for loop; innermost variable "wins"

Here is a demo of a running C program, and a C++ program failing to compile.

Opening a nested scope inside the body fixes the compile error (demo):

for (int i =0 ; i != 5 ; i++) {
    {
        int i = 10;
        cout << i << endl;
    }
}

Now i in the for header and int i = 10 are in different scopes, so the program is allowed to run.

這篇關于在循環中重新聲明 for 循環變量時出錯的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 99国产精品99久久久久久粉嫩 | 日日干干 | 国产裸体永久免费视频网站 | 一区二区三区成人 | 日韩a在线 | 国产日韩视频 | av男人天堂网 | 久久久久久成人 | 91爱爱爱 | 婷婷av在线 | 日日日干干干 | 性巴克成人免费网站 | 国产精品久久久一区二区 | 一级肉体裸体bbbb | 亚洲永久免费视频 | 午夜一级视频 | 久久一级视频 | 99久久精品国产毛片 | 97超碰免费 | 国产精品欧美日韩 | 成人精品一区二区三区 | 久久久九九 | 成av人片一区二区三区久久 | 久热99 | 欧美精品久久久久久 | 久久精品一二三 | www.日本在线| 自拍偷拍综合 | 免费的黄色大片 | 日韩在线视频免费 | 久久精品久久久久久久 | 自拍偷在线精品自拍偷无码专区 | 国产传媒视频在线观看 | 国产美女在线播放 | 午夜精品久久久久久 | 九九九精品视频 | 久艹视频在线观看 | 欧美精品区 | www.亚洲天堂 | 国产精品高潮呻吟久久 | 超碰在线看 |