未声明未使用的变量.gcc取笑我?

klm*_*123 0 c++ macos gcc compiler-errors

我的代码中有下一行:

const int xxx = hhh.IR(); // line 234
if( !aaaaaaa[iT][xxx] ) // line 235
Run Code Online (Sandbox Code Playgroud)

编译器抱怨:

/FILE.cxx:234:21: warning: unused variable 'xxx' [-Wunused-variable]
/FILE.cxx:235:30: error: 'xxx' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

(它取笑我吗?)这怎么可能?

我检查了两个都xxx使用相同的字符,只需复制一个xxx并用它替换另一个.

PS:我有

  • gcc 4.7.3
  • OSX 10.9.

klm*_*123 7

当第一行和第二行的范围不同时,这是可能的.

这里是一个代码,它会给出这样的错误:

if ( y == z )
  const int xxx = hhh.IR();
  if( !aaaaaaa[iT][xxx] )
Run Code Online (Sandbox Code Playgroud)

等于:

if ( y == z ) {
  const int xxx = hhh.IR();
}
if( !aaaaaaa[iT][xxx] )
Run Code Online (Sandbox Code Playgroud)

所以修复是添加大括号:

if ( y == z ) {
  const int xxx = hhh.IR();
  if( !aaaaaaa[iT][xxx] )
Run Code Online (Sandbox Code Playgroud)

  • 他们被称为"大括号". (2认同)