I stumbled on a problem I was able to work around, but I’m not sure why it wasn’t working.
This is the code I tried to use. Fields have been stripped for brevity. Let me know if they’re needed and I’ll put them back:
#pragma once
#ifndef __PageStyle__
#define __PageStyle__
class PageStyle
{
public:
friend bool operator<(const PageStyle& lhs, const PageStyle& rhs);
};
bool operator<(const PageStyle& lhs, const PageStyle& rhs)
{
return (lhs.name < rhs.name);
}
#endif
And in my source files I do something like this:
#include "PageStyle.h"
...
void PageStyleManager::loadPageStyles() {
std::set<PageStyle> pageStyles;
...
}
The code compiled fine, but the linker spat this out:
1>PageStyleManager.obj : error LNK2005: "bool __cdecl operator<(class PageStyle const &,class PageStyle const &)" (??M@YA_NABVPageStyle@@0@Z) already defined in BaseContentFiller.obj
BaseContentFiller is the base class for PageStyleManager, as well as for other classes that also use PageStyle in a similar way.
After delving a bit further I discovered that for my purposes (using the class in an STL set) I didn’t really need a non-member friend version after all. I made the operator an in-line public member and the code linked without a problem.
Why did this problem occur though? I ensured I had used header guards, this is my first real experience with operator overloading and I’d like to know what I did wrong.
If you include the definition of the function in the header file, it gets defined in every Translation unit where the header file is included.
This violates the One Definition Rule and hence the linker error.
Note that the header guards or the
#pragma oncejust prevent the same header file from being included in the same source file multiple times, but not defining it across different TU.Two solutions are possible to solve the problem:
cppfile orinline.