我有这样漂亮的小码:为什么编译器接受含糊变量定义?
//example1
namespace
{
int a;
}
int a;
main()
{
a++;
return 0;
}
当然,G ++ 4.6.1编译器不能编译并输出一个错误:
./temp.cpp: In function ‘int main()’:
./temp.cpp:10:5: error: reference to ‘a’ is ambiguous
./temp.cpp:6:5: error: candidates are: int a
./temp.cpp:2:9: error: int {anonymous}::a
没关系!
但是,当我删除提及变量“一个”内“主”的功能,该程序正在编译以及:
//example2
namespace
{
int a;
}
int a;
main()
{
return 0;
}
1)为什么克++编译器允许的定义变量“a”,在这种情况下,它不允许引用它?
2)它只是g ++编译器的功能,没有其他编译器能够编译这样的代码(example2)?
3)g ++编译器是否有相应的标志来解释这样的代码(example2)为错误?
非常感谢大家!
真的! :)我想,在这样的变量的“a”用法中,它应该放在源代码的末尾...... – 2013-04-30 13:48:21
它应该放置在最有意义的位置...... – 2013-04-30 13:54:25