【发布时间】:2012-10-30 12:37:41
【问题描述】:
可能重复:
Is it possible to declare two variables of different types in a for loop?
考虑这段代码:
for( std::vector<std::string>::iterator it = myVec.begin(), int i = 3; it < myVec.end(); it++, i++ )
{
// some operations with the vector
}
我很惊讶 MSVC 2010 在编译这个 for 循环时给了我错误。 MSVS 禁止使用逗号运算符吗?
这是一个错误:
error C2062: type 'int' unexpected
error C2143: syntax error: missing ';' before ')'
如果我尝试将“int i”定义排除在循环之外,我会得到:
error C2440: 'initializing': cannot convert from 'int' to 'std::vector'
【问题讨论】:
-
您在尝试将其排除在外时输入错误。如果您不显示该代码,我们将无法帮助您修复它。
-
@Mat,似乎一切都井井有条。刚检查。谢谢。
-
“一切似乎都井井有条” - 如果是,你的编译器不会告诉你它不是。
-
我必须同意马特的观点。我编写了与您的代码非常相似的代码,并且编译时没有错误或警告。 (当然是在 MSVC 中)
标签: c++ visual-studio comma comma-operator