why it is wrong?

K

kathy

I have code piece:

....
std::fstream outFile(_T("data.txt"),std::ios::eek:ut|std::ios::trunc);
if(outFile)
{
....
}

When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."

Why?
 
V

Victor Bazarov

kathy said:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::eek:ut|std::ios::trunc);

"_T" thing is non-standard. Could it be you're in some non-standard
territory?
if(outFile)
{
...
}

When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."

Why?

Because the compiler is bad?... Hard to say. This code:

#include <fstream>

int main() {
std::fstream o("blah", std::ios::eek:ut);
if (o)
return 0;
else
return 1;
}

Should compile fine. Try it. If it fails, contact the compiler vendor.

V
 
M

Mike Wahler

kathy said:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::eek:ut|std::ios::trunc);

Note that the '_T' macro is not part of standard C++ (and
is not germane to your question).
if(outFile)
{
...
}

When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."

Why?

There's an idiom (and a supporting member function) for checking
the state of a stream, which allows a stream object to be used
in a boolean context. But converting it to a boolean value
doesn't 'just happen'. To do so requires the use of a boolean
logical operator (a stream's 'value' itself is not boolean).
(The member function is 'operator void*', you can research it
if you like).

Try this

if(!outFile)
// stream in is error state
else
// stream is in 'good' state

If you want the first test to be for 'good' state, you
can write:

if(!!outFile)

or:

if(outFile.good())


BTW if you only need output, you should use 'std::eek:fstream'.
'std::fstream' is intended for streams which are both read
from and written to.

-Mike
 
A

Alf P. Steinbach

* kathy wrote in [comp.lang.c++]:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::eek:ut|std::ios::trunc);
if(outFile)
{
...
}

When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."

Why?

It's a compiler (or rather, standard library implementation) bug;
however, I'm unable to find a relevant bug-list at Dinkumware.

As a workaround you can use "!!", as mentioned by others, or e.g.

std::eek:fstream outFile( "data.txt", std::ios::eek:ut|std::ios::trunc );

There are two differences from what you wrote. The second one is a
correction of a bug that could prevent your code from compiling (adding
in platform-specific details: when you define UNICODE). The first one
fixes the problem you ran into this time; do you see what it is?

CC: P.J.Plauger
 
P

Pete Becker

kathy said:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::eek:ut|std::ios::trunc);
if(outFile)
{
...
}

When I build it, I got:
"c:\temp\DialogDemoDlg.cpp(202): error C2451: conditional expression of
type 'std::fstream' is illegal."

Compiler bug. The code is okay, aside from the _T thingy. fstream
doesn't have a constructor that takes wchar_t, so you always want char*.
That doesn't affect this, though.
 
P

Pete Becker

Mike said:
There's an idiom (and a supporting member function) for checking
the state of a stream, which allows a stream object to be used
in a boolean context. But converting it to a boolean value
doesn't 'just happen'. To do so requires the use of a boolean
logical operator (a stream's 'value' itself is not boolean).
(The member function is 'operator void*', you can research it
if you like).

It does just happen when the object is used in a boolean context, such
as if(outFile). The rest of the error message makes the problem clearer.
The compiler goes on to say that there's an ambiguous user-defined
conversion. That's a compiler bug: operator void* is defined in a class
that's a virtual base of both ostream and istream, and fstream is
derived from both (skipping a few steps here and there...)
 
M

Mike Wahler

Pete Becker said:
It does just happen when the object is used in a boolean context, such as
if(outFile). The rest of the error message makes the problem clearer. The
compiler goes on to say that there's an ambiguous user-defined conversion.
That's a compiler bug: operator void* is defined in a class that's a
virtual base of both ostream and istream, and fstream is derived from both
(skipping a few steps here and there...)

I stand corrected. Thanks.

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top