IO Stream Library usage

  • Thread starter Thread starter Joe Greene
  • Start date Start date
J

Joe Greene

Compiling this file produces the "no appropriate default constructor available" error
shown below. Can anyone explain why I'm getting this error and how to fix it???

This source file has been reduced to the bare essentials to reproduce the error.

/* test.cpp */
#include <ostream>

class LogStream : public std::ostream, public std::streambuf
{
public:
LogStream ();
};

LogStream :: LogStream ()
{
init(this);
};
/* end of test.cpp */

------ Build started: Project: test, Configuration: Debug Win32 ------

Compiling...
test.cpp
\Projects\test\test99\test.cpp(10) : error C2512: 'std::basic_ostream<_Elem,_Traits>' :
no appropriate default constructor available
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

Build log was saved at "file://d:\Projects\test\test99\test\Debug\BuildLog.htm"
test - 1 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped
 
Joe said:
Compiling this file produces the "no appropriate default constructor available" error
shown below. Can anyone explain why I'm getting this error and how to fix it???

This source file has been reduced to the bare essentials to reproduce the error.

/* test.cpp */
#include <ostream>

class LogStream : public std::ostream, public std::streambuf
{
public:
LogStream ();
};

LogStream :: LogStream ()
{
init(this);
};

ostream has no default constructor. In addition, you shouldn't ever call
init yourself if you are deriving from ostream, since ostream does it
for you (calling it twice causes a memory leak at the very least).

Needs to be something like:
LogStream::LogStream()
:std::ostream(0) //calls init(0)
{
rdbuf(this); //reset streambuf
clear(); //clear badbit set by calling init(0)
}

Or you should reorder the base classes so that the streambuf is created
before the ostream:
class LogStream : public std::streambuf, public std::ostream
{
public:
LogStream ();
};
LogStream::LogStream()
:std::ostream(this) //calls init
{
}

Tom
 
Tom,
Thank you very much! That eliminated my compile error. Do you know a good book
to recommend that covers the standard IO Streams in depth?

Thanks again,
Joe
 
Joe said:
Tom,
Thank you very much! That eliminated my compile error. Do you know a good book
to recommend that covers the standard IO Streams in depth?

The comprehensive text is "C++ IOStreams and Locales" by Langer and
Kreft. Apparently it's good, but I haven't read it personally.
Alternatively there are books on the whole of standard C++ or the whole
C++ standard library that cover it in less detail, such as "The C++
Standard Library" by Josuttis - much of the IOStreams section was
written by Dietmar Kuehl, an expert who regularly posts on
comp.lang.c++.moderated.

Finally, check out the book reviews on www.accu.org, which are more
reliable than those on, say, Amazon.

Tom
 
Back
Top