473,225 Members | 1,277 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,225 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 3973
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: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.