error: 'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoffrey S. Knauth

    #1

    error: 'std::ios_base& std::ios_base::operator=(const std::ios_base&)' is private

    It's been a while since I programmed in C++, and the language sure has
    changed. Usually I can figure out why something no longer compiles, but
    this time I'm stumped. A friend has a problem he hoped I could solve,
    and I couldn't. Some code he's using, written in 1999, that compiled
    fine in 1999, no longer does in 2006 with g++ 4.

    This little bit of code:

    SimS::SimS (ostream &s)
    {
    rc = 1;
    Stream = s;
    is_opened = 0;
    }

    SimS::SimS (SimS &s)
    {
    rc = 1;
    Stream = s.Stream;
    is_opened = 0;
    }

    produces quite a few errors, such as these:

    simstream.c: In constructor 'SimS::SimS(std ::ostream&)':
    /usr/include/c++/4.0.0/ostream:360: error: 'std::basic_ost ream<_CharT,
    _Traits>::basic _ostream() [with _CharT = char, _Traits =
    std::char_trait s<char>]' is protected
    simstream.c:17: error: within this context
    /usr/include/c++/4.0.0/iosfwd: In member function 'std::basic_ios <char,
    std::char_trait s<char> >& std::basic_ios< char, std::char_trait s<char>[color=blue]
    >::operator=(co nst std::basic_ios< char, std::char_trait s<char> >&)':[/color]
    /usr/include/c++/4.0.0/bits/ios_base.h:782: error: 'std::ios_base&
    std::ios_base:: operator=(const std::ios_base&) ' is private
    /usr/include/c++/4.0.0/iosfwd:55: error: within this context
    /usr/include/c++/4.0.0/iosfwd: In member function
    'std::basic_ost ream<char, std::char_trait s<char> >&
    std::basic_ostr eam<char, std::char_trait s<char> >::operator=(co nst
    std::basic_ostr eam<char, std::char_trait s<char> >&)':
    /usr/include/c++/4.0.0/iosfwd:64: warning: synthesized method
    'std::basic_ios <char, std::char_trait s<char> >& std::basic_ios< char,
    std::char_trait s<char> >::operator=(co nst std::basic_ios< char,
    std::char_trait s<char> >&)' first required here
    simstream.c: In constructor 'SimS::SimS(std ::ostream&)':
    simstream.c:20: warning: synthesized method 'std::basic_ost ream<char,
    std::char_trait s<char> >& std::basic_ostr eam<char,
    std::char_trait s<char> >::operator=(co nst std::basic_ostr eam<char,
    std::char_trait s<char> >&)' first required here
    simstream.c: In copy constructor 'SimS::SimS(Sim S&)':
    /usr/include/c++/4.0.0/ostream:360: error: 'std::basic_ost ream<_CharT,
    _Traits>::basic _ostream() [with _CharT = char, _Traits =
    std::char_trait s<char>]' is protected

    How can this be fixed?

    Thanks.

    --
    Geoffrey S. Knauth | http://knauth.org/gsk

    A little more background:

    class SimS {
    public:
    SimS (ostream &s);
    SimS (SimS& s);
    SimS (const char* f);
    ~SimS (void);
    void flush (void);
    private:
    int rc; // reference count
    ofstream fp; // file pointer
    ostream Stream; // stream
    bool is_opened; // a file was opened
    int attached (void) { return ++rc; }
    int detached (void) { return --rc; }
    friend class SimStream; // only SimStream accesses the
    above
    };

    class SimStream {
    public:
    SimStream (ostream &s, char *p = NULL);
    SimStream (SimStream& s, char *p = NULL);
    SimStream (const char* f, char *p = NULL);
    ~SimStream (void);
    bool reopen (SimStream& s, char* p = NULL);
    bool reopen (const char* f, char* p = NULL);
    void flush (void);
    SimStream& operator= (SimStream& s);
    SimStream& operator<< (const int i);
    SimStream& operator<< (const unsigned int i);
    SimStream& operator<< (const unsigned long int i);
    SimStream& operator<< (const long int i);
    SimStream& operator<< (const char* s);
    SimStream& operator<< (const char c);
    SimStream& operator<< (const float f);
    SimStream& operator<< (const double d);
    SimStream& operator<< (ostream& o(ostream&));
    SimStream& put (char c) { return *this; }
    void width (int i) { entry->Stream.width(i ); }
    void fill (char c) { entry->Stream.fill(c) ; }
    void setf (unsigned long val, unsigned long mask) {
    entry->Stream.setf( (ios_base::fmtf lags)val,
    (ios_base::fmtf lags)mask); }
    void precision (int i) { entry->Stream.precisi on(i); }
    private:
    SimS* entry;
    char* prompt;
    bool is_echoed; // the prompt was echoed
    void attach (SimS* s);
    void detach (void);
    };
  • Jacek Dziedzic

    #2
    Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)'is private

    Geoffrey S. Knauth wrote:[color=blue]
    > It's been a while since I programmed in C++, and the language sure has
    > changed. Usually I can figure out why something no longer compiles, but
    > this time I'm stumped. A friend has a problem he hoped I could solve,
    > and I couldn't. Some code he's using, written in 1999, that compiled
    > fine in 1999, no longer does in 2006 with g++ 4.
    >
    > This little bit of code:
    >
    > SimS::SimS (ostream &s)
    > {
    > rc = 1;
    > Stream = s;
    > is_opened = 0;
    > }
    >
    > SimS::SimS (SimS &s)
    > {
    > rc = 1;
    > Stream = s.Stream;
    > is_opened = 0;
    > }
    >
    > produces quite a few errors, such as these:[/color]

    Show how "Stream" is defined and what header files you
    include. Posting a small _compilable_ chunk of code
    that exhibits the behaviour is good.

    - J.

    Comment

    • Victor Bazarov

      #3
      Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)'is private

      Geoffrey S. Knauth wrote:[color=blue]
      > It's been a while since I programmed in C++, and the language sure has
      > changed.[/color]

      Not that much.
      [color=blue]
      > Usually I can figure out why something no longer compiles, but
      > this time I'm stumped. A friend has a problem he hoped I could solve,
      > and I couldn't. Some code he's using, written in 1999, that compiled
      > fine in 1999, no longer does in 2006 with g++ 4.
      >
      > This little bit of code:
      >
      > SimS::SimS (ostream &s)
      > {
      > rc = 1;
      > Stream = s;[/color]

      You're not supposed to copy streams. If your class keeps a _reference_
      to a stream as a member, _initialise_ it, don't assign it.
      [color=blue]
      > is_opened = 0;
      > }
      >
      > SimS::SimS (SimS &s)
      > {
      > rc = 1;
      > Stream = s.Stream;[/color]

      Same story.
      [color=blue]
      > is_opened = 0;
      > }
      >
      > [..][/color]

      V

      Comment

      • Bob Hairgrove

        #4
        Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)' is private

        On Tue, 17 Jan 2006 14:31:07 -0500, geoff@knauth.or g (Geoffrey S.
        Knauth) wrote:
        [color=blue]
        >This little bit of code:
        >
        >SimS::SimS (ostream &s)
        >{
        > rc = 1;
        > Stream = s;
        > is_opened = 0;
        >}
        >
        >SimS::SimS (SimS &s)
        >{
        > rc = 1;
        > Stream = s.Stream;
        > is_opened = 0;
        >}
        >
        >produces quite a few errors, such as these:[/color]

        [snip]

        Some questions arise:

        1. What headers are included? (Note: headers with a trailing "*.h" can
        cause problems, especially WRT i/o-streams)
        2. Where do you declare namespace std?
        [color=blue]
        >class SimS {
        > public:
        > // SimS (ostream &s);[/color]
        That should be:
        SimS (std::ostream &s);[color=blue]
        > // SimS (SimS& s);[/color]
        That should probably be:
        SimS (SimS const & s);
        [color=blue]
        > SimS (const char* f);
        > // ~SimS (void);[/color]
        That should be:
        ~SimS ();[color=blue]
        > // void flush (void);[/color]
        That should be:
        void flush ();[color=blue]
        > private:
        > int rc; // reference count
        > // ofstream fp; // file pointer[/color]
        That should be:
        std::ofstream fp; // file pointer[color=blue]
        > ostream Stream; // stream[/color]
        That should be:
        std::ostream Stream; // stream[color=blue]
        > bool is_opened; // a file was opened
        > int attached (void) { return ++rc; }
        > int detached (void) { return --rc; }
        > friend class SimStream; // only SimStream accesses the
        >above
        >};[/color]

        --
        Bob Hairgrove
        NoSpamPlease@Ho me.com

        Comment

        • Geoffrey S. Knauth

          #5
          Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)' is private

          Victor Bazarov <v.Abazarov@com Acast.net> wrote:[color=blue][color=green]
          > > Stream = s;[/color]
          > You're not supposed to copy streams. If your class keeps a _reference_
          > to a stream as a member, _initialise_ it, don't assign it.[/color]

          Thanks. Will try.

          --
          Geoffrey S. Knauth | http://knauth.org/gsk

          Comment

          • Geoffrey S. Knauth

            #6
            Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)' is private

            Jacek Dziedzic <jacek@no_spam. tygrys.no_spam. net> wrote:[color=blue]
            > Show how "Stream" is defined and what header files you
            > include. Posting a small _compilable_ chunk of code
            > that exhibits the behaviour is good.[/color]

            The two files causing problems are shown below. Thanks.

            --
            Geoffrey S. Knauth | http://knauth.org/gsk

            --->-8 --- simstream.h -----------------------------------------8-<---
            #ifndef __simstream_h__
            #define __simstream_h__

            #include <iostream>
            #include <fstream>
            using namespace std;

            class SimS {
            public:
            SimS (ostream &s);
            SimS (SimS& s);
            SimS (const char* f);
            ~SimS (void);
            void flush (void);
            private:
            int rc; // reference count
            ofstream fp; // file pointer
            ostream Stream; // stream
            bool is_opened; // a file was opened
            int attached (void) { return ++rc; }
            int detached (void) { return --rc; }
            friend class SimStream; // only SimStream accesses the
            above
            };

            class SimStream {
            public:
            SimStream (ostream &s, char *p = NULL);
            SimStream (SimStream& s, char *p = NULL);
            SimStream (const char* f, char *p = NULL);
            ~SimStream (void);
            bool reopen (SimStream& s, char* p = NULL);
            bool reopen (const char* f, char* p = NULL);
            void flush (void);
            SimStream& operator= (SimStream& s);
            SimStream& operator<< (const int i);
            SimStream& operator<< (const unsigned int i);
            SimStream& operator<< (const unsigned long int i);
            SimStream& operator<< (const long int i);
            SimStream& operator<< (const char* s);
            SimStream& operator<< (const char c);
            SimStream& operator<< (const float f);
            SimStream& operator<< (const double d);
            SimStream& operator<< (ostream& o(ostream&));
            SimStream& put (char c) { return *this; }
            void width (int i) { entry->Stream.width(i ); }
            void fill (char c) { entry->Stream.fill(c) ; }
            void setf (unsigned long val, unsigned long mask) {
            entry->Stream.setf( (ios_base::fmtf lags)val,
            (ios_base::fmtf lags)mask); }
            void precision (int i) { entry->Stream.precisi on(i); }
            private:
            SimS* entry;
            char* prompt;
            bool is_echoed; // the prompt was echoed
            void attach (SimS* s);
            void detach (void);
            };
            --->-8 --- simstream.c -----------------------------------------8-<---
            #include <iostream>
            #include <fstream>
            #include <string>

            #include "simstream. h"

            using namespace std;

            SimS::SimS (ostream &s)
            {
            rc = 1;
            Stream = s;
            is_opened = 0;
            }

            SimS::SimS (SimS &s)
            {
            rc = 1;
            Stream = s.Stream;
            is_opened = 0;
            }

            SimS::SimS (const char* f)
            {
            rc = 1;
            if (f) {
            fp.open(f);
            if (fp.bad() || fp.fail()) {
            cerr << "Can't open `" << f << "', `cout' will replace it"
            << endl;
            Stream = cout;
            is_opened = 0;
            }
            else {
            Stream = fp;
            is_opened = 1;
            }
            }
            else {
            cerr << "Can't open NULL file, `cout' will replace it" << endl;
            Stream = cout;
            is_opened = 0;
            }
            }

            SimS::~SimS (void)
            {
            if (is_opened) fp.close();
            is_opened = 0;
            }

            void SimS::flush (void)
            {
            fp.flush();
            }

            void SimStream::atta ch (SimS* s)
            {
            entry = s;
            entry->attached();
            }

            void SimStream::deta ch (void)
            {
            if (entry->detached() == 0) delete entry;
            }

            SimStream::SimS tream (ostream& s, char* p)
            {
            entry = new SimS(s);
            prompt = p ? strdup(p) : p;
            is_echoed = 0;
            }

            SimStream::SimS tream (SimStream& s, char* p)
            {
            this->attach(s.entry );
            prompt = p ? strdup(p) : p;
            is_echoed = 0;
            }

            SimStream::SimS tream (const char* f, char* p)
            {
            entry = new SimS(f);
            prompt = p ? strdup(p) : p;
            is_echoed = 0;
            }

            SimStream::~Sim Stream (void)
            {
            this->detach();
            }

            bool SimStream::reop en (SimStream& s, char* p)
            {
            this->detach();
            this->attach(s.entry );
            prompt = p ? strdup(p) : p;
            is_echoed = 0;
            if (entry->is_opened) return 1;
            else return 0;
            }

            bool SimStream::reop en (const char* f, char* p)
            {
            this->detach();
            entry = new SimS(f);
            prompt = p ? strdup(p) : p;
            is_echoed = 0;
            if (entry->is_opened) return 1;
            else return 0;
            }

            void SimStream::flus h (void)
            {
            entry->flush();
            }

            SimStream& SimStream::oper ator= (SimStream& s)
            {
            this->detach();
            this->attach(s.entry );
            return *this;
            }

            SimStream& SimStream::oper ator<< (const int i)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << i;
            return *this;
            }

            SimStream& SimStream::oper ator<< (const unsigned long int i)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << i;
            return *this;
            }

            SimStream& SimStream::oper ator<< (const unsigned int i)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << i;
            return *this;
            }

            SimStream& SimStream::oper ator<< (const long int i)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << i;
            return *this;
            }

            SimStream& SimStream::oper ator<< (const char c)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << c;
            if (c == '\n') is_echoed = 0; // reset
            return *this;
            }

            SimStream& SimStream::oper ator<< (const char* s)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << s;
            for (int i = 0; i < strlen(s); i++)
            if (s[i] == '\n') is_echoed = 0; // reset
            return *this;
            }

            SimStream& SimStream::oper ator<< (const float f)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << f;
            return *this;
            }

            SimStream& SimStream::oper ator<< (const double d)
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << d;
            return *this;
            }

            SimStream& SimStream::oper ator<< (ostream& o(ostream&))
            {
            if (prompt)
            if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
            1; }
            entry->Stream << o;
            is_echoed = 0; // reset
            return *this;
            }

            Comment

            • Earl Purple

              #7
              Re: error: 'std::ios_base& amp; std::ios_base:: operator=(const std::ios_base&a mp;)' is private


              Geoffrey S. Knauth wrote:[color=blue]
              > It's been a while since I programmed in C++, and the language sure has
              > changed. Usually I can figure out why something no longer compiles, but
              > this time I'm stumped. A friend has a problem he hoped I could solve,
              > and I couldn't. Some code he's using, written in 1999, that compiled
              > fine in 1999, no longer does in 2006 with g++ 4.
              >
              > This little bit of code:
              >
              > SimS::SimS (ostream &s)
              > {
              > rc = 1;
              > Stream = s;
              > is_opened = 0;
              > }
              >
              > SimS::SimS (SimS &s)
              > {
              > rc = 1;
              > Stream = s.Stream;
              > is_opened = 0;
              > }
              >[/color]
              < big snip >

              The whole thing is flawed - why is he trying to rewrite streams?

              If you really want, write your own derivation of ostream. Note that
              ostream actually is a template: basic_ostream< char >. Also note that
              ostream itself has no virtual members at all apart from its destructor,
              and that ostream takes a streambuf* pointer in its constructor, so what
              you actually do is write your own derivation of streambuf (actually
              basic_streambuf <char> ) and pass that as a pointer to the constructor
              of ostream.

              If what you want is a wrapper for an ostream that is copyable then that
              is basic using boost::shared_p tr (or std::tr1::share d_ptr), i.e.

              class ostream_wrapper
              {
              private:
              boost::shared_p tr< ostream > itsStream;

              public:
              operator ostream&() ( return *itsStream; }
              };

              plus constructors to get the ostream in there in the first place.

              Comment

              Working...