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

  • Home
  • SEARCH
  • 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 6810161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:10:22+00:00 2026-05-26T20:10:22+00:00

I’m trying to overload operator<< in the standard way. I have a class called

  • 0

I’m trying to overload operator<< in the standard way. I have a class called SymbolTable residing in a file called SymbolTable.h as follows:

namespace Compaler // It's a pun; don't ask
{

class SymbolTable
{
public:
  // ...
  friend ostream& operator<<(ostream &out, const SymbolTable &table);
private:
  vector<map<int, Symbol*> > mSymbols;
};

// ...

}

The implementation of operator<< accesses some private members of SymbolTable. Here is the signature of the implementation, to demonstrate that it does not differ from the signature of the forward declaration (I copy-pasted it to be sure I wasn’t going crazy.)

ostream& operator<<(ostream &out, const SymbolTable &table)
{ ... }

If I put the implementation in SymbolTable.h where the second … is, I get the following linker error:

ld: duplicate symbol Compaler::operator<<(std::basic_ostream >&, Compaler::SymbolTable const&)in /var/folders/kt/pl6qd1490fn3yqxfpg64jyr80000gn/T//ccstrYnU.o and /var/folders/kt/pl6qd1490fn3yqxfpg64jyr80000gn/T//ccDQFiyK.o for architecture x86_64

However, if I instead put it in SymbolTable.cpp, where the rest of my SymbolTable member implementations are, the code won’t even compile; I get errors related to accessing private members, indicating that the friendship is not being recognized:

../SymbolTable/SymbolTable.h: In function ‘std::ostream& operator<<(std::ostream&, const Compaler::SymbolTable&)’:
../SymbolTable/SymbolTable.h:75: error: ‘std::vector, std::allocator >, Compaler::Symbol*, std::less, std::allocator > >, std::allocator, std::allocator >, Compaler::Symbol*> > >, std::allocator, std::allocator >, Compaler::Symbol*, std::less, std::allocator > >, std::allocator, std::allocator >, Compaler::Symbol*> > > > > Compaler::SymbolTable::mSymbols’ is private
../SymbolTable/SymbolTable.cpp:98: error: within this context

What am I doing wrong?

Summary of answers:

Thanks to Seth Carnegie and Daniel R. Hicks, I’ve got it figured out. For the benefit of anyone who might make the same mistakes in the future, I’m going to summarize the answers in my question because there was a lot of back and forth in the comments.

The first (duplicate symbol when the implementation was in SymbolTable.h) problem occurred because I was trying to simultaneously compile and link two files which included SymbolTable.h . I have been labouring under a misunderstanding of how #ifdef include guards work; that is, they will only prevent code from being included twice in one file, but will not help you if you try to link two files which both included code from a third.

The second problem was that I forgot to put my operator<< implementation in the Compaler namespace when I had it in the SymbolTable.cpp file.

  • 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-26T20:10:23+00:00Added an answer on May 26, 2026 at 8:10 pm

    The first problem is because you’re putting the definition in the header file and #includeing it twice. This causes a multiple definition error.

    As for the second problem, probably your function has a different signature than the friend declaration.

    Just to let you know, as an alternative to getting the signature right, you can write the function inline:

    class SymbolTable
    {
    public:
      // ...
      friend ostream& operator<<(ostream &out, const SymbolTable &table) {
          ...
      }
    private:
      vector<map<int, Symbol*> > mSymbols;
    };
    

    Edit: Since apparently the signature is right, the error lies somewhere else. You’ll have to post more code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a reasonable size flat file database of text documents mostly saved in
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.