G
Geoffrey S. Knauth
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:stream&)':
/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::ios_base:perator=(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> >:perator=(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> >:perator=(const std::basic_ios<char,
std::char_traits<char> >&)' first required here
simstream.c: In constructor 'SimS::SimS(std:stream&)':
simstream.c:20: warning: synthesized method 'std::basic_ostream<char,
std::char_traits<char> >& std::basic_ostream<char,
std::char_traits<char> >:perator=(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);
};
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:stream&)':
/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,
/usr/include/c++/4.0.0/bits/ios_base.h:782: error: 'std::ios_base&std::char_traits said::perator=(const std::basic_ios<char, std::char_traits<char> >&)':
std::ios_base:perator=(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> >:perator=(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> >:perator=(const std::basic_ios<char,
std::char_traits<char> >&)' first required here
simstream.c: In constructor 'SimS::SimS(std:stream&)':
simstream.c:20: warning: synthesized method 'std::basic_ostream<char,
std::char_traits<char> >& std::basic_ostream<char,
std::char_traits<char> >:perator=(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);
};