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