简体   繁体   English

基于范围的循环声明必须声明一个变量

[英]Range based loop declaration must declare a variable

BYTE Function::Function(long MyInt)
{
    std::vector<int> Red = { 6, 8 };

    for (MyInt : Red)
    {
        return 1;
    }

    return 0;
}

This is my function. 这是我的职责 I want to use MyInt intro for range, but I get a strange error. 我想将MyInt介绍用于范围,但是出现一个奇怪的错误。

I get this error ; 我得到这个错误; error: for range 错误:范围

declaration must declare a variable

What is wrong here? 怎么了 I can't understand. 我听不懂

What are you trying to do? 你想做什么? This doesn't make sense to me, why would you start a loop to end it immediately by returning? 这对我来说没有意义,为什么您要开始循环以通过返回立即结束循环?

Well, you can't use an existing variable in a ranged for loop, you have to declare a new one: 好吧,您不能在有范围的for循环中使用现有变量,而必须声明一个新变量:

for (long myint : Red)
{
    //...
}

You could also use the same name for (long MyInt : Red) , but that can confuse people into thinking that MyInt and MyInt are the same variables, which isn't good. 您也可以for (long MyInt : Red)使用相同的名称,但是这会使人们MyInt以为MyIntMyInt是相同的变量,这不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM