473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_ostream<_CharT,
_Traits>::basic_ostream() [with _CharT = char, _Traits =
std::char_traits<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_traits<char> >& std::basic_ios<char, std::char_traits<char>
::operator=(const std::basic_ios<char, std::char_traits<char> >&)':

/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_ostream<char, std::char_traits<char> >&
std::basic_ostream<char, std::char_traits<char> >::operator=(const
std::basic_ostream<char, std::char_traits<char> >&)':
/usr/include/c++/4.0.0/iosfwd:64: warning: synthesized method
'std::basic_ios<char, std::char_traits<char> >& std::basic_ios<char,
std::char_traits<char> >::operator=(const std::basic_ios<char,
std::char_traits<char> >&)' first required here
simstream.c: In constructor 'SimS::SimS(std::ostream&)':
simstream.c:20: warning: synthesized method 'std::basic_ostream<char,
std::char_traits<char> >& std::basic_ostream<char,
std::char_traits<char> >::operator=(const std::basic_ostream<char,
std::char_traits<char> >&)' first required here
simstream.c: In copy constructor 'SimS::SimS(SimS&)':
/usr/include/c++/4.0.0/ostream:360: error: 'std::basic_ostream<_CharT,
_Traits>::basic_ostream() [with _CharT = char, _Traits =
std::char_traits<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::fmtflags)val,
(ios_base::fmtflags)mask); }
void precision (int i) { entry->Stream.precision(i); }
private:
SimS* entry;
char* prompt;
bool is_echoed; // the prompt was echoed
void attach (SimS* s);
void detach (void);
};
Jan 17 '06 #1
6 9983
Geoffrey S. Knauth wrote:
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:


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.
Jan 17 '06 #2
Geoffrey S. Knauth wrote:
It's been a while since I programmed in C++, and the language sure has
changed.
Not that much.
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;
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.
is_opened = 0;
}

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

[..]


V
Jan 17 '06 #3
On Tue, 17 Jan 2006 14:31:07 -0500, ge***@knauth.org (Geoffrey S.
Knauth) wrote:
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:
[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?
class SimS {
public:
// SimS (ostream &s); That should be:
SimS (std::ostream &s); // SimS (SimS& s); That should probably be:
SimS (SimS const & s);
SimS (const char* f);
// ~SimS (void); That should be:
~SimS (); // void flush (void); That should be:
void flush (); private:
int rc; // reference count
// ofstream fp; // file pointer That should be:
std::ofstream fp; // file pointer ostream Stream; // stream That should be:
std::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
};


--
Bob Hairgrove
No**********@Home.com
Jan 17 '06 #4
Victor Bazarov <v.********@comAcast.net> wrote:
Stream = s;

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.


Thanks. Will try.

--
Geoffrey S. Knauth | http://knauth.org/gsk
Jan 18 '06 #5
Jacek Dziedzic <jacek@no_spam.tygrys.no_spam.net> wrote:
Show how "Stream" is defined and what header files you
include. Posting a small _compilable_ chunk of code
that exhibits the behaviour is good.


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::fmtflags)val,
(ios_base::fmtflags)mask); }
void precision (int i) { entry->Stream.precision(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::attach (SimS* s)
{
entry = s;
entry->attached();
}

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

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

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

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

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

bool SimStream::reopen (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::reopen (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::flush (void)
{
entry->flush();
}

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

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

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

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

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

SimStream& SimStream::operator<< (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::operator<< (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::operator<< (const float f)
{
if (prompt)
if (! is_echoed) { entry->Stream << prompt << ' '; is_echoed =
1; }
entry->Stream << f;
return *this;
}

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

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

Jan 18 '06 #6

Geoffrey S. Knauth wrote:
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;
}

< 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_ptr (or std::tr1::shared_ptr), i.e.

class ostream_wrapper
{
private:
boost::shared_ptr< ostream > itsStream;

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

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

Jan 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
4
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927...
9
by: Thomas J. Clancy | last post by:
I was wondering if anyone knew of a way to use std::copy() and istream_iterator<>/ostream_iterator<> write a file copy function that is quick and efficient. Doing this messes up the file because...
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
6
by: idesilva | last post by:
Hi, I have an application which sends/receives messages at a very high rate. This app needs to 'log' the contents of these messages. Since the msg rate is high, 'logging' each and every msg to a...
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
2
by: bosse | last post by:
Hi, i have got a linker problem, i don't know how to handle; there are three projects in my workspace. In the Project called modules_common is a class called Features. In the second project...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.