473,395 Members | 1,456 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,395 software developers and data experts.

ostream constructor error

I have this piece of code that is working fine in VC6... but when i tried to
compile the same code in VC++.NET, I am getting this error :

error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate default
constructor available
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

i had also changed #include <fstream.h> to #include <fstream>
and added this line "using namespace std;"

following is the relevant code : the arrow shows the line at which the error
is being generated
i understand that the libraries might have changed during the upgrade from
6.0 0 7.0. can you tell me what is the appropriate method to fix the code
again?
i would also appreciate if you can tell me about any sites that let you how
to fix these kind of errors that come from 6.0 - > 7.0

thanks,
Julian.

#include <fstream>
using namespace std;
class ArgumentOstream : public ostream
{
private:
ofstream *ostrm;
bool streamOwner;
public:
ArgumentOstream(Arguments &args, ostream &o=cout) <------------------
{
....
}
init(o.rdbuf());
}
~ArgumentOstream() {if(streamOwner) ostrm->close();}
};

Jan 31 '06 #1
5 4101
Julian wrote:
I have this piece of code that is working fine in VC6... but when i
tried to compile the same code in VC++.NET, I am getting this error :

error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate
default constructor available
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

i had also changed #include <fstream.h> to #include <fstream>
and added this line "using namespace std;"

Ok, but do you understand why these changes are necessary? VC6 used a
non-standard "stream" implementation, whereas CV7 conforms to what is
dictated in the C++ standard concerning streams. The documentation for
standard ostream is here :
http://msdn.microsoft.com/library/de...asp?frame=true
It shows us that std::ostream is really a typedef for
std::basic_ostream<char>, and the only constructor for this typeis defined
here :
http://msdn.microsoft.com/library/de...asp?frame=true
As you can see, the constructor needs a basic_streambuf argument. Therefore,
you need to provide a streambuf for the constructin of the base clas ostream
of your ArgumentOstream class. Not knowing your whole code,I can't be sure,
but U guess your constructor should be :

ArgumentOstream(Arguments &args, ostream &o=cout)

: ostream(o.rdbuf())
{
//no call to init : it is done through the base class constructor
//...
}

Arnaud
MVP - VC
Feb 1 '06 #2

"Arnaud Debaene" <ad******@club-internet.fr> wrote in message
news:Oq**************@TK2MSFTNGP15.phx.gbl...
Julian wrote:
I have this piece of code that is working fine in VC6... but when i
tried to compile the same code in VC++.NET, I am getting this error :

error C2512: 'std::basic_ostream<_Elem,_Traits>' : no appropriate
default constructor available
with
[
_Elem=char,
_Traits=std::char_traits<char>
]

i had also changed #include <fstream.h> to #include <fstream>
and added this line "using namespace std;"

Ok, but do you understand why these changes are necessary? VC6 used a
non-standard "stream" implementation, whereas CV7 conforms to what is
dictated in the C++ standard concerning streams. The documentation for
standard ostream is here :
http://msdn.microsoft.com/library/de...asp?frame=true
It shows us that std::ostream is really a typedef for
std::basic_ostream<char>, and the only constructor for this typeis defined
here :
http://msdn.microsoft.com/library/de...asp?frame=true
As you can see, the constructor needs a basic_streambuf argument.
Therefore, you need to provide a streambuf for the constructin of the base
clas ostream of your ArgumentOstream class. Not knowing your whole code,I
can't be sure, but U guess your constructor should be :

ArgumentOstream(Arguments &args, ostream &o=cout)

: ostream(o.rdbuf())
{
//no call to init : it is done through the base class constructor
//...
}

Arnaud
MVP - VC

I understand it was upgraded to conform to Standard C++. but i don't
understand the specific reason for each change. but that doesn't matter.
Actually, i didn't realize what exactly was causing the error. I assumed
that it was because of
ArgumentOstream(Arguments &args, ostream &o=cout)
/\

but now i realize that it was basically because of the base class ostream:
class ArgumentOstream : public ostream
/\

your solution fixes the problem, but that was not the intended action..
anyway, now that i understand the problem, i think i can fix it properly...
Thank you very much for your help !
Feb 2 '06 #3
i have one more problem. the old code contains an class called
ostream_withassign

I could not find any mention of this class in the standard c++ library. i
used ostream instead:

BEFORE:
class formStreamBuf: public streambuf
{
protected:
ostream_withassign o;
public:
formStreamBuf(ostream &_o) {
o=_o;
....

AFTER:
class formStreamBuf: public streambuf
{
protected:
ostream o;
public:
formStreamBuf(ostream &_o) : o(_o.rdbuf()) { ...

Is this the correct way to fix this problem ?
Feb 2 '06 #4
Julian wrote:
i have one more problem. the old code contains an class called
ostream_withassign

I could not find any mention of this class in the standard c++ library. i
used ostream instead:

BEFORE:
class formStreamBuf: public streambuf
{
protected:
ostream_withassign o;
public:
formStreamBuf(ostream &_o) {
o=_o;
...

AFTER:
class formStreamBuf: public streambuf
{
protected:
ostream o;
public:
formStreamBuf(ostream &_o) : o(_o.rdbuf()) { ...

Is this the correct way to fix this problem ?


Yes - IIRC the lifetime issues are the same for both bits of code (the
ostream passed to the constructor needs to outlast the formStreamBuf).

Tom
Feb 2 '06 #5

"Tom Widmer [VC++ MVP]" <to********@hotmail.com> wrote in message
news:ee****************@TK2MSFTNGP11.phx.gbl...
Julian wrote:
i have one more problem. the old code contains an class called
ostream_withassign

I could not find any mention of this class in the standard c++ library. i
used ostream instead:

BEFORE:
class formStreamBuf: public streambuf
{
protected:
ostream_withassign o;
public:
formStreamBuf(ostream &_o) {
o=_o;
...

AFTER:
class formStreamBuf: public streambuf
{
protected:
ostream o;
public:
formStreamBuf(ostream &_o) : o(_o.rdbuf()) { ...

Is this the correct way to fix this problem ?


Yes - IIRC the lifetime issues are the same for both bits of code (the
ostream passed to the constructor needs to outlast the formStreamBuf).

Tom


thanks, appreciate your help !
Feb 3 '06 #6

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

Similar topics

8
by: Boris | last post by:
Is it possible to manipulate the std::ostream to prepend a string when performing output, e.g. // manipute std::cout to prepend "prefix " std::cout << "hallo" << std::endl; // results in...
6
by: pembed2003 | last post by:
Hi all, Given something like: std::ofstream out_file("path"); how do I extract the file descriptor from out_file? Is it possible? What I want is to extract the file descriptor and then pass...
7
by: Robert Sherry | last post by:
It is my understanding that the standard variable cout is of type ostream. Please consider the following C++ program. #include <iostream> using namespace std; void func1( ostream os1 ); ...
13
by: Angel Tsankov | last post by:
How do I define a null ostream that inherits publicly std::ostream and ignores anything that would otherwise be output?
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
5
by: lars | last post by:
Hi, I have C (printf) style debug functions static void debugPrint(Silver::Strategy& s, unsigned level, const char *format, ...) { if (s.verbosityLevel() >= level ) { va_list args ;...
4
by: Art Werschulz | last post by:
Hi. I would like one of the constructors for a given class to take an ostream as a parameter. The following example (foo.cc) doesn't work: ...
6
davydany
by: davydany | last post by:
hey guys, i'm in desperate need of your help...well this is a program I'm doing for my Data Structures class. We need to do recursive functions. well the first assignment has this prototype: void...
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.