简体   繁体   中英

Weird Linker error from overloaded << operator - C++

I wrote a small Timer class to check my code's performance. The sourcecode can be found here http://pastebin.com/i1PX2VPN (I'll hightlight important sections later here)

Now I faced a very weird error: The code linked above is in a file Timer.hpp. I use this file in my main.cpp and it works just fine. However, I also want to use it in another source file, and then, as soon as I add a

  #include <Timer.hpp> 

into the next source file, I get the following linker error:

  ld: duplicate symbol operator<<(std::basic_ostream<char, std::char_traits<char> >&, Timer const&) in /var/folders/XZ/XZ93KWBqG0SR1aCVpTCVQE+++TI/-Tmp-//ccyaHyyU.o and /var/folders/XZ/XZ93KWBqG0SR1aCVpTCVQE+++TI/-Tmp-//ccETAaTw.o
  collect2: ld return 1 as exit status
  make: *** [debugd] Error 1

The function is

 std::ostream& operator<<(std::ostream& os, const Timer& t) {
         return os << std::scientific << std::setw(8)  << std::setprecision(3)
                            << t.timeTotal_ << " \t " << t.timeMin_ << " \t " <<          t.timeMax_;
   }

Nothing too fancy... again: It works as long as it is only included in my main.cpp

Obviously this seems to be a problem with multiple definitions (?) of the operator. But I cannot understand why or how to solve it. I use include-guards to save it from being included multiple times.

I already checked whether there is another Timer class anywhere else in the C++-library I never heard of, which is obviously not the case (renaming the class doesn't change anything anyway)...

In the Timer.hpp, there is also another class defined, also with its own operator<< overloaded. I don't know whether this is important, I just think I should mention it

So... I'm looking to forward to your ideas, thanks

You should mark your function as inline , since it is a global function whose definition is included by more than one translation unit.

    inline std::ostream& operator<<(std::ostream& os, const Timer& t) {
//  ^^^^^^
         return os << std::scientific << std::setw(8)  << std::setprecision(3)
               << t.timeTotal_ << " \t " << t.timeMin_ << " \t " 
               << t.timeMax_;
    }

Alternatively, you can leave the declaration of the function in the header, and put the definition in one single .cpp file.

If you are wondering why include guards are not saving you from multiple symbol definitions, you may find this Q&A on StackOverflow useful.

Include guards only prevent a file from being included multiple times in the same translation unit . If you are including it in two different .cpp files that you compile seperately (which .cpp files usually are), you have multiple definitions.

Put it in an implementation file, leaving only a declaration in the header.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM