简体   繁体   English

在不同源文件中声明的同名类没有编译器/链接器错误

[英]No compiler/linker error for same name classes declared in different source files

I have 2 files as below, which declare a same name class C : 我有以下两个文件,它们声明了相同的名称class C

// C1.cpp
class C { 
public: void foo (int, float);
};

int main () {
  C c;
  c.foo(1, 2.3);
}

void C::foo (int, float) {}

// C2.cpp
class C { 
public:
  int i;  // <--- extra variable
  void foo (int, float);  // <--- non static
};

void foo () {
  C c;
  c.foo(0, 0.0);
  c.i = 0;
}

It compiles fine with g++ C1.cpp C2.cpp ! 使用g++ C1.cpp C2.cpp可以编译良好!

  1. Why is there no compiler/linker error when only the names are same but the bodies are not ? 当只有名称相同但主体不相同时,为什么没有编译器/链接器错误?
  2. Why the single definition of C::foo(..) serves for both the static and non-static versions? 为什么C::foo(..)的单一定义可同时用于static和非静态版本?
  3. Or if this is just another undefined behavior case from compiler, can we prevent it? 或者,如果这只是编译器的另一个未定义的行为案例,我们可以阻止它吗?

Your program violates One definition rule and has undefined behaviour. 您的程序违反了一个定义规则,并且具有未定义的行为。 From n3337 3.2/5 , emphasis mine: n3337 3.2/5 ,重点是:

There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data member of a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.5) in a program provided that each definition appears in a different translation unit , and provided the definitions satisfy the following requirements. 一个类类型 (第9章),枚举类型(7.2),具有外部链接的内联函数(7.1.2),类模板(第14条),非静态函数模板(14.5.6)可以有多个定义。 ,类模板的静态数据成员(14.5.1.3),类模板的成员函数(14.5.1.1)或未在程序中指定某些模板参数(14.7、14.5.5)的模板专门化, 前提是每个定义出现在不同的翻译单元中 ,并且提供的定义满足以下要求。 Given such an entity named D defined in more than one translation unit, then 给定这样一个名为D的实体,它在多个翻译单元中定义,则

each definition of D shall consist of the same sequence of tokens; D的每个定义应由相同的令牌序列组成; and

in each definition of D, corresponding names, looked up according to 3.4, shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution (13.3) and after matching of partial template specialization (14.8.3), except that a name can refer to a const object with internal or no linkage if the object has the same literal type in all definitions of D, and the object is initialized with a constant expression (5.19), and the value (but not the address) of the object is used, and the object has the same value in all definitions of D; 在D的每个定义中,按照3.4查找的对应名称,应指代D定义内定义的实体,或应在重载解析 (13.3)和部分模板专业化匹配后指代同一实体 ( 14.8.3),但如果名称在D的所有定义中都具有相同的文字类型,并且该对象使用常量表达式(5.19)初始化,则该名称可以引用具有内部链接或不具有链接的const对象,并且该值(但不使用地址)使用该对象,并且该对象在D的所有定义中都具有相同的值; and

[ ... omitted stuff that's not directly related to the example ... ] [ ...省略了与示例不直接相关的内容... ]

If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined . 如果D的定义满足所有这些要求,则程序应表现为好像有一个D的定义。如果D的定义不满足这些要求,则行为是不确定的

You can't get a compiler error because it only "sees" one translation unit at a time. 您不会得到编译器错误,因为它一次只能“看到”一个翻译单元。 The linker and it's error messages are out of the scope of C++ standard, so I believe there can't be a de iure answer why it doesn't diagnose. 链接器及其错误消息不在C ++标准范围之内,因此我认为无法给出无法诊断的答案。 I guess it just takes the first C symbol that encounters and discards the rest. 我猜它只需要遇到的第一个C符号并丢弃其余的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在两个不同的cpp文件中定义具有相同名称的类时,为什么没有链接器错误? - Why there is no linker error while defining classes with the same name in two different cpp files? 两个同名文件在 Visual Studio 中出现链接器错误 - Two files of the same name give linker error in Visual Studio GCC-具有相同名称的不同类的错误 - GCC - error for different classes with the same name C++ 两个模板,相同的名称,相同的签名,不同的类:如何强制编译器使用预期的一个? - C++ Two templates, same name, same signatures, different classes: How to force compiler to use intended one? 编译器如何区分C ++中不同类中具有相同名称的静态数据成员? - How can a compiler differentiate between static data members having same name in different classes in C++? Makefile包含源文件的不同编译器标志 - Makefile with different compiler flags for source files 编译器/链接器错误 - Compiler/linker error 为什么AIX上的xlC编译器总是为相同的c ++源代码生成不同的目标文件? - Why xlC compiler on aix always generates different object files for the same c++ source code? 不同名称空间中具有相同名称的类 - Classes with same name in different namespace 编译器错误:未在作用域中声明 - Compiler error: not declared in the scope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM