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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:46:06+00:00 2026-06-16T00:46:06+00:00

I’m trying to make a C++ class resembling std::ostream , that will take its

  • 0

I’m trying to make a C++ class resembling std::ostream, that will take its input and write to two std::ostreams given in the constructor. Here it is together with appropriate operator<< template:

struct SplitStream
{
    SplitStream(std::ostream & a_, std::ostream & b_) : a(a_), b(b_) {}
    std::ostream & a, & b;
};


template<class T>
const SplitStream & operator << (const SplitStream & sp, const T & x)
{
    sp.a << x;
    sp.b << x;
    return sp;
}

Several lines below that code, I try to use this class:

void foo(SplitStream & out)
{
    double some_double = 1.23;
    out << "bar" << some_double << std::endl;
}

And I get this rather enigmatic error:

... error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'const SplitStream' (or there is no acceptable conversion) ...

What am I doing wrong? I tried to define operator<< without consts, and it didn’t compile either.

  • 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-16T00:46:07+00:00Added an answer on June 16, 2026 at 12:46 am

    The immediate problem is that std::endl isn’t an object but a function template declared something like this:

    template <typename cT, typename Traits>
    std::basic_ostream<cT, Traits>& endl(std::basic_ostream<cT, Traits>&);
    

    To use a function pointer like this, the template arguments need to be deduced. To this end, the class std::basic_ostream<cT, Traits> declared suitable overloads for operator<<():

    template <typename cT, typename Traits>
    std::basic_ostream<cT, Traits>& std::baisic_ostream<cT, Traits>::operator<< (
        std::basic_ostream<cT, Traits>& (*manip)(std::basic_ostream<cT, Traits>&));
    

    This way, the compiler can deduce the correct instantiation when the std::endl function is referenced.

    However, all that is utterly irrelevant because what you are trying to do is better done entirely differently! You should create a suitable stream buffer and use a reasonably constructed std::ostream with this custom stream buffer. Below is a complete example how to do it properly (I had posted it before but only a couple dozens times…):

    #include <streambuf>
    
    struct teebuf
        : std::streambuf
    {
        std::streambuf* sb1_;
        std::streambuf* sb2_;
    
        teebuf(std::streambuf* sb1, std::streambuf* sb2)
            : sb1_(sb1), sb2_(sb2) {
        }
        int overflow(int c) {
            typedef std::streambuf::traits_type traits;
            bool rc(true);
            if (!traits::eq_int_type(traits::eof(), c)) {
                traits::eq_int_type(this->sb1_->sputc(c), traits::eof())
                    && (rc = false);
                traits::eq_int_type(this->sb2_->sputc(c), traits::eof())
                    && (rc = false);
            }
            return rc? traits::not_eof(c): traits::eof();
        }
        int sync() {
            bool rc(true);
            this->sb1_->pubsync() != -1 || (rc = false);
            this->sb2_->pubsync() != -1 || (rc = false);
            return rc? 0: -1;
        }
    };
    
    #include <fstream>
    #include <iostream>
    
    int main()
    {
        std::ofstream fout("tee.txt");
        teebuf        sbuf(fout.rdbuf(), std::cout.rdbuf());
        std::ostream  out(&sbuf);
        out << "hello, world!\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.