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

时间:2013-12-04 16:28:03

标签: c++ macos gcc compiler-errors

我的代码中有下一行:

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

编译器抱怨:

/FILE.cxx:234:21: warning: unused variable 'xxx' [-Wunused-variable]
/FILE.cxx:235:30: error: 'xxx' was not declared in this scope

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

我检查过两个xxx使用相同的字符,只需复制一个xxx并替换其他字符。

P.S .: 我有

  • gcc 4.7.3
  • OSX 10.9。

1 个答案:

答案 0 :(得分:7)

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

这里是一个会产生错误的代码示例:

if ( y == z )
  const int xxx = hhh.IR();
  if( !aaaaaaa[iT][xxx] )

等于:

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

所以修复是添加大括号:

if ( y == z ) {
  const int xxx = hhh.IR();
  if( !aaaaaaa[iT][xxx] )