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 8869215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:26:02+00:00 2026-06-14T17:26:02+00:00

I am trying to overload the stream operator <<, for a class Foo which

  • 0

I am trying to overload the stream operator <<, for a class Foo which has already a toString() function returning a string, with the following code:

std::ostream &operator<<( std::ostream &flux, Foo const& foo )
{
    flux <<  foo.toString(); 
    return flux;
}

In order to use it in a main.cppfile

My question is: Where to put that piece of code?

  • If I place it in the main.cpp, before its usage, it works well, but i may want to use it in other files.
  • If I place it in foo.cpp, I get a ‘no such function’ error:

    src/main.cpp:77: error: no match for ‘operator<<’ in ‘std::cout << foo’
    

    which make sense since the code is not included to the main.cpp file

  • If I place it in the foo.hclass header, outside class declaration, I get a ‘multiple definition’ error:

    foo.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Foo const&)':
    foo.cpp:(.text+0x0): multiple definition of `operator<<(std::basic_ostream<char, std::char_traits<char> >&, Matrix const&)'
    bar.o:bar.cpp:(.text+0x0): first defined here
    

    The foo.h header is indeed included in different classes/files, but there is a ifdef guard, so I don’t understand this.

So How should I do?

  • 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-06-14T17:26:04+00:00Added an answer on June 14, 2026 at 5:26 pm

    There are multiple options:

    Declare it in the header, after Foo, and define it in Foo.cpp.

    //foo.h
    class Foo
    {};
    std::ostream &operator<<( std::ostream &s, Foo const& foo );
    
    //foo.cpp
    #include "foo.h"
    std::ostream &operator<<( std::ostream &s, Foo const& foo )
    {
        return s;
    }
    

    Define it as a friend inside the class definition.

    //Foo.h
    class Foo
    {
       friend std::ostream &operator<<( std::ostream &s, Foo const& foo )
       {
          return s;
       }
    };
    

    Define it in the header, outside the class definition, and mark it as inline to prevent the multiple definition.

    //Foo.h
    class Foo
    {
    };
    
    inline std::ostream &operator<<( std::ostream &s, Foo const& foo )
    {
       return s;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to overload the '--' postfix operator. I have this code: class Counter
I'm trying to overload the dereference operator, but compiling the following code results in
Can I overload existing function/operator in existing class? I was trying to do: #include
I am trying to overload the subscript operator ([]) on an abstract class, the
I am trying to overload operator<< as a member function. It works if simply
I am trying to overload the += operator for my rational number class, but
I'm trying to overload operator<< in the standard way. I have a class called
I'm trying to overload the operator << as a friend to a template class
I am trying to overload the stream insertion operator so can I print std::vector
I'm trying to read a single character from a stream. With the following code

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.