Do compilers always complain? I have never seen what I did, and the standard does not allow this: you can define a class or template once in each translation unit, if all the definitions are identical. In fact, you need to define a class in each translation unit that uses it in such a way that it is a full type. C ++ has no mechanism for exporting a definition class for other translation units.
Are you sure you are not confusing classes with functions. You are not allowed to define a function more than once, unless it is built-in. You still need to define a function template in each translation unit that uses it, and the same rules apply for function templates as for classes and class templates.
Please note that if you break these rules by defining a function in more than one translation unit, or if class definitions or templates are not identical (after preprocessing and including name binding), then you have undefined behavior. The compiler (actually the linker) may complain about this, but this is not required: most complain about several function definitions, but I donβt know anyone who complains when the class or template definitions differ from each other in translation units.
source share