473,396 Members | 2,154 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,396 software developers and data experts.

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 3978
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Jay Levitt | last post by:
I'm just starting to play around with CSS and MovableType. My home page (http://www.jay.fm) now validates on both the CSS and the XHTML. However, the Google cached version shows the wrong font in...
7
by: Jerry Krinock | last post by:
I've declared a class that has some std::vector data members like this: class MyClass { public: ... std::vector<Apples> apples ; ... private: ...
5
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
5
by: Krisnamourt Correia via SQLMonster.com | last post by:
I have one query that executes many times in a week. I created one Maintenances plan that Rebuild all index in my Database that has been executed at 23:40 Saturday until stop finished at Sunday. ...
6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
3
by: Soren Jorgensen | last post by:
Hi, Following code should give the number of weeks in years 1998-2010 for a Danish calendar (on a Danish box) GregorianCalendar cal = new GregorianCalendar(); for(int i = 1998; i < 2010; i++)...
8
by: Dmitry Korolyov | last post by:
ASP.NET app using c# and framework version 1.1.4322.573 on a IIS 6.0 web server. A single-line asp:textbox control and regexp validator attached to it. ^\d+$ expression does match an empty...
3
by: belton180 | last post by:
CODE]../* Program function: Simulate the stack using a stack limit of 10. Display a menu for the the following. Create a stack Insert an item in the stack Pop an item from the stack ...
318
by: jacob navia | last post by:
Rcently I posted code in this group, to help a user that asked to know how he could find out the size of a block allocated with malloc. As always when I post something, the same group of people...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.