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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:41:37+00:00 2026-06-12T18:41:37+00:00

I need to output messages to both console and log files. After googling, i

  • 0

I need to output messages to both console and log files.
After googling, i learnt “teebuf” concept which basically create a custom class inherited from basic_streambuf. This work fine but how to clean up the redirected buffer properly. I mean how to implement RAII for “tee” buffer so i don’t need to bother it every time when i need to quit the program.

Remark: currently using boost library is not a option for me.

Code Segment

int main() {
    std::streambuf* cout_sbuf = std::cout.rdbuf();
    std::ofstream   fout(fileOutDebug);
    teeoutbuf       teeout(std::cout.rdbuf(), fout.rdbuf());
    std::cout.rdbuf(&teeout);

    // some code ...
    if (failed) {
        std::cout.rdbuf(cout_sbuf); // <-- i once comment this and it gives error
        return -1;
    }

    // some more code ...
    std::cout.rdbuf(cout_sbuf); // <-- i once comment this and it gives error
    return 0;
}

Code Segment (My trial implementation, but fail)

template < typename CharT, typename Traits = std::char_traits<CharT>
> class basic_tostream : std::basic_ostream<CharT, Traits>
{
    public:
        basic_tostream(std::basic_ostream<CharT, Traits> & o1,
                       std::basic_ostream<CharT, Traits> & o2)
    : std::basic_ostream<CharT, Traits>(&tbuf), 
      tbuf(o1.rdbuf(), o2.rdbuf()) {}
        void print(char* msg);
    private:
    basic_teebuf<CharT, Traits> tbuf; // internal buffer (tee-version)
};
typedef basic_tostream<char> tostream;

int main() {
    std::ofstream fout(fileOutDebug);
    tostream tee(std::cout, fout, verbose);
    tee << "test 1\n"; // <-- compile error
    tee.print("sometext"); // <-- comment above and it run fine, both file and console written
}

Error message: ‘std::basic_ostream’ is an inaccessible base of ‘basic_tostream’

  • 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-12T18:41:38+00:00Added an answer on June 12, 2026 at 6:41 pm

    You should use:

    template < typename CharT, typename Traits = std::char_traits<CharT> >
    class basic_tostream : public std::basic_ostream<CharT, Traits>
    

    instead of:

    template < typename CharT, typename Traits = std::char_traits<CharT> >
    class basic_tostream : std::basic_ostream<CharT, Traits>
    

    Key difference being public. That’s what the 'std::basic_ostream' is an inaccessible base of 'basic_tostream' error message is about.

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

Sidebar

Related Questions

I need to log debugging messages from the same class into different files. What
I have a C# MVC application which I need to output the following data
I'm writing C++ code to output midi messages. I need this to work in
I need to output a file in the format of a Word document from
I need to output the days of the week in a JSP. Clearly I
I need to output the lines of a file where the first column's values
I need to output some text as bash script, but in a script. I
It has suddenly happened that I need to output a couple of strings in
I'm writing an XSLT template that need to output a valid xml file for
I have a Drupal 7 site and need to output a small calendar control

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.