468,241 Members | 1,718 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

why it is wrong?

I have code piece:

....
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
if(outFile)
{
....
}

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

Why?

Feb 8 '06 #1
7 3740
kathy wrote:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
"_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::out);
if (o)
return 0;
else
return 1;
}

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

V
--
Please remove capital As from my address when replying by mail
Feb 8 '06 #2

"kathy" <yq*****@yahoo.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
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::ofstream'.
'std::fstream' is intended for streams which are both read
from and written to.

-Mike


Feb 8 '06 #3
* kathy wrote in [comp.lang.c++]:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
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::ofstream outFile( "data.txt", std::ios::out|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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 8 '06 #4
Alf P. Steinbach wrote:

It's a compiler (or rather, standard library implementation) bug;


Right the first time: compiler bug.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 9 '06 #5
kathy wrote:
I have code piece:

...
std::fstream outFile(_T("data.txt"),std::ios::out|std::ios::tru nc);
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 9 '06 #6
Mike Wahler wrote:

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...)

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 9 '06 #7

"Pete Becker" <pe********@acm.org> wrote in message
news:4s********************@giganews.com...
Mike Wahler wrote:

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...)


I stand corrected. Thanks.

-Mike
Feb 9 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Jay Levitt | last post: by
7 posts views Thread by Jerry Krinock | last post: by
5 posts views Thread by Krisnamourt Correia via SQLMonster.com | last post: by
6 posts views Thread by Michael Sparks | last post: by
3 posts views Thread by Soren Jorgensen | last post: by
8 posts views Thread by Dmitry Korolyov | last post: by
3 posts views Thread by belton180 | last post: by
318 posts views Thread by jacob navia | last post: by
3 posts views Thread by Siong.Ong | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.