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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:22:32+00:00 2026-06-18T11:22:32+00:00

I am getting this error when trying to compile my code: 1> c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/ostream: In

  • 0

I am getting this error when trying to compile my code:

1>  c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/ostream: In constructor 'Log::Log(const char*)':
1>c:\mingw\bin\..\lib\gcc\mingw32\4.6.2\include\c++\ostream(363,7): error : 'std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char, _Traits = std::char_traits<char>]' is protected
1>C:\Users\Adam\Documents\cs\cs176b\hw2\ftp\ftp\log.cpp(6,26): error : within this context

Not really sure why, I didn’t make the ostream code I am using myself, I used what was suggested here: Proper way to create ostream to file or cout

My code is below, happy to give any more info if needed:

// log.h
#include <string>
#include <fstream>

#ifndef LOG_H_
#define LOG_H_

class Log 
{
    public:
        enum Mode { STDOUT, FILE };

        // Needed by default
        Log(const char *file = NULL);
        ~Log();

        // Writing methods
        void write(char *);
        void write(std::string);
    private:
        Mode mode;
        std::streambuf *buf;
        std::ofstream of;
        std::ostream out;
};

#endif


// log.cpp
#include "log.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>

Log::Log(const char *file)
{
    if (file != NULL)
    {
        of.open(file);
        buf = of.rdbuf();
        mode = FILE;
    }
    else
    {
        buf = std::cout.rdbuf();
        mode = STDOUT;
    }

    // Attach to out
    out.rdbuf(buf);
}

Log::~Log()
{
    if (mode == FILE)
        of.close();
}

void Log::write(std::string s)
{
    out << s << std::endl;
}

void Log::write(char *s)
{
    out << s << std::endl;
}
  • 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-18T11:22:33+00:00Added an answer on June 18, 2026 at 11:22 am

    std::ostream::ostream() constructor is protected, which means it can only be invoked by its derived class, but not but its enclosing one.

    To fix the error initialize member out.

    The only public constructor of std::ostream accepts a std::streambuf*, e.g.:

    Log::Log(const char *file)
        : out(std::cout.rdbuf())
    // ...
    

    Note, that it is safe to initialize a std::ostream with the buffer from std::cout.rdbuf(), because destructor std::ostream::~ostream() does not deallocate its std::streambuf* member.

    Alternatively, it can be initialized with a NULL/nullptr. In this case be careful to not output anything into the stream as it would try dereferencing that NULL leading to undefined behaviour, most likely just crash with SIGSEGV.

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

Sidebar

Related Questions

I'm trying to compile the next code, but I'm getting this error on the
I was getting this error /usr/local/cuda-5.0/bin/../include/host_config.h:82:2: error: #error -- unsupported GNU version! gcc 4.7
So I keep getting this error when trying to compile C++ code using CodeBlocks.
Am getting this compiler error while trying to compile an VS 6 VC++ code.
I am trying to compile my C code but I'm getting this error, I've
Getting this error while trying to compile a copied code from a Fortran 77
This is the error I'm getting when trying to compile some code that uses
Trying to compile a iPhone/iPad application with SDK3.2 and am getting this error: Undefined
I keep getting this error while trying to modify some tables. Here's my code:
I'm trying to compile this code under linux with gcc compiler : static inline

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.