multiple definition in header file

2019-01-06 12:59发布

问题:

Given this code sample:

complex.h :

#ifndef COMPLEX_H
#define COMPLEX_H

#include <iostream>

class Complex
{
public:
   Complex(float Real, float Imaginary);

   float real() const { return m_Real; };

private:
   friend std::ostream& operator<<(std::ostream& o, const Complex& Cplx);

   float m_Real;
   float m_Imaginary;
};

std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}
#endif // COMPLEX_H

complex.cpp :

#include "complex.h"

Complex::Complex(float Real, float Imaginary) {
   m_Real = Real;
   m_Imaginary = Imaginary;
}

main.cpp :

#include "complex.h"
#include <iostream>

int main()
{
   Complex Foo(3.4, 4.5);
   std::cout << Foo << "\n";
   return 0;
}

When compiling this code, I get the following error:

multiple definition of operator<<(std::ostream&, Complex const&)

I've found that making this function inline solves the problem, but I don't understand why. Why does the compiler complain about multiple definition? My header file is guarded (with #define COMPLEX_H).

And, if complaining about the operator<< function, why not complain about the public real() function, which is defined in the header as well?

And is there another solution besides using the inline keyword?

回答1:

The problem is that the following piece of code is a definition, not a declaration:

std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}

You can either mark the function above and make it "inline" so that multiple translation units may define it:

inline std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
   return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
}

Or you can simply move the original definition of the function to the "complex.cpp" source file.

The compiler does not complain about "real()" because it is implicitly inlined (any member function whose body is given in the class declaration is interpreted as if it had been declared "inline"). The preprocessor guards prevent your header from being included more than once from a single translation unit ("*.cpp" source file"). However, both translation units see the same header file. Basically, the compiler compiles "main.cpp" to "main.o" (including any definitions given in the headers included by "main.cpp"), and the compiler separately compiles "complex.cpp" to "complex.o" (including any definitions given in the headers included by "complex.cpp"). Then the linker merges "main.o" and "complex.o" into a single binary file; it is at this point that the linker finds two definitions for a function of the same name. It is also at this point that the linker attempts to resolve external references (e.g. "main.o" refers to "Complex::Complex" but does not have a definition for that function... the linker locates the definition from "complex.o", and resolves that reference).



回答2:

And is there another solution as using the inline keyword?

Yes, there is. Apart form defining the method inside the implementation file complex.cpp as mentioned by others, you can also put the definition into a nameless namespace.

namespace {
    std::ostream& operator<<(std::ostream& o, const Complex& Cplx) {
        return o << Cplx.m_Real << " i" << Cplx.m_Imaginary;
    }
}

In practice, this will create a unique namespace for each compilation unit. That way, you prevent the name clashes. However, the names are still exported from the compilation unit but useless (since the names are unknown).

Putting the definition inside the implementation file is often a better solution. However, for class templates you can’t do that since C++ compilers don’t support instantiating templates in a compilation unit different from the one they were defined in. In those case, you have to use either inline or an unnamed namespace.



回答3:

Move implementation to complex.cpp

Right now after including this file implementation is being compiled to every file. Later during linking there's a obvious conflict because of duplicate implementations.

::real() is not reported because it's inline implicitly (implementation inside class definition)



回答4:

I was having this problem, even after my source and header file were correct.

It turned out Eclipse was using stale artifacts from a previous (failed) build.

To fix, use Project > Clean then rebuild.