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.