ostream constructor error

  • Thread starter Thread starter Julian
  • Start date Start date
J

Julian

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();}
};
 
Julian said:
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/d...lib/html/vclrf_ostream_Ostream.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/d...tream_basicostreambasicostream.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
 
Arnaud Debaene said:
Julian said:
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/d...lib/html/vclrf_ostream_Ostream.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/d...tream_basicostreambasicostream.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 !
 
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 ?
 
Julian said:
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
 
Tom Widmer said:
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 !
 
Back
Top