Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:29:48+00:00 2026-05-29T13:29:48+00:00

I stumbled on a problem I was able to work around, but I’m not

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:29:49+00:00Added an answer on May 29, 2026 at 1:29 pm

    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 once just 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:

    1. Just add the declaration in the header file and the definition in only one cpp file or
    2. make the function defined in the header as inline.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using Azure CDN, but we've stumbled upon a problem. Before, content could not
I'm doing some Delphi (2010) work this summer, and I've stumbled upon this problem:
I stumbled across this problem in F#. Suppose, I want to declare two types
I stumbled upon a problem of how to make work together acts_as_taggable (on steroids)
I'm quite new to hibernate and have stumbled on this problem, which I can't
This is a problem one of our developers brought to me. He stumbled across
With the new iPhone 2x files i've stumbled uppon this problem...
I am new to Qt and stumbled across a problem that I could not
DISCLAIMER: The following code is not something I would ever use in a real
I've stumbled into a problem which, at a glance, is easy cplusplusable, but on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.