46

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?

4
  • You can also make the function static. The inline specifier is commonly used to force a function to have internal linkage.
    – Akanksh
    Apr 28, 2010 at 7:43
  • @Akanksh, actually that's exactly what "inline" is for. Apr 28, 2010 at 7:54
  • @Akanksh: Using static for this purpose is deprecated in C++. static has been completely replaced by anonymous namespaces, although in this particular case, inline is the way to go. Apr 28, 2010 at 7:56
  • @Akanksh: That invites ODR violations by giving the same name (operator<<) different meanings in different translation units. Mar 30, 2020 at 21:11

4 Answers 4

79

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).

6

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)

1

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.

0

An alternative to designating a function definition in a header file as inline is to define it as static. This will also avoid the multiple definition error.

1
  • 1
    That makes the functions have internal linkage - different from inline. In particular, this will cause ODR violations (ill-formed NDR) if an inline function tries to call them in different translation units.
    – L. F.
    Apr 6, 2020 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.