为什么在同一声明区域内声明相同的名称被拒绝? [英] Why does declaration of the same name inside the same declarative region is denied?

查看:167
本文介绍了为什么在同一声明区域内声明相同的名称被拒绝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法编译:

#include <iostream>
#include <stdio.h>

int a=5;
char a='a';

int main(){ std::cout << a;}

这是因为:

test.cpp:5:6: error: conflicting declaration ‘char a’
test.cpp:4:5: error: ‘a’ has a previous declaration as ‘int a’

但是标准中指定的限制在哪里?我找不到它。请给我一个参考。

But where does this restriction specified in the standard? I can't find it. Please give me a reference.

推荐答案

这是对单一定义规则(ODR)的简单违反。参见n3797 S3.2 / 1。

This is a simple violation of the One Definition Rule (ODR). See n3797 S3.2/1.


无翻译单元应包含任何变量,函数,类类型,枚举类型,或模板。

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

真的没有什么可说的了。每一行都是 a (不是声明)的定义

There really is no more to be said. Each of those lines is a definition of a (not a declaration). The program is ill-formed.

如果这些声明使用的是不同的翻译单元,程序仍然是不合格的,但适用的规则是不同的。见S3.5 / 10。

If these declarations were in different translation units the program would still be ill-formed, but the applicable rule is different. See S3.5/10.

这篇关于为什么在同一声明区域内声明相同的名称被拒绝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆