Does anyone knows how I can serialize it. I know I can use BOOL but I
need to use bool.
Thanks
My guess is that MFC doesn't know anything about the bool data type.
Be sure to send a big thank-you to Microsoft for that.
There are various ways to get around it, depending on your situation.
Also, depending on your situation, it may or may not be on-topic for
this group. A better bet is probably microsoft.public.vc.language or
microsoft.public.vc.mfc.
_______
"Yields falsehood when preceded by its quotation" yields falsehood when preceded by its quotation.
Josh Sebastian
<cur...@earthlink.net>
I suspect you're leaving out some context.
> >Does anyone knows how I can serialize it. I know I can use BOOL but I
> >need to use bool.
> >
> >Thanks
>
> My guess is that MFC doesn't know anything about the bool data type.
> Be sure to send a big thank-you to Microsoft for that.
Why do I always get sucked into defending MS? :-)
#include <iostream>
#include <fstream>
int main()
{
bool flag = true;
std::ofstream output("test.txt");
if(!output)
{
std::cout << "can't open output file" << std::endl;
return EXIT_FAILURE;
}
output << flag << std::endl;
return 0;
}
Compiled and linked with MSVC++ v6.0 SP4
This results in a file containing the text of the
numeral one (1), followed by a newline. By making
adjustments to a locale's facet, you can also cause
this output to be 'true', instead of '1'. (I'll let
you look it up :-) )
And this works whether or not MFC headers are included.
> There are various ways to get around it,
I don't think there's anything to 'get around'.
> depending on your situation.
Well perhaps older versions of VC don't have the native
'bool' type, I don't know.
-Mike
>Josh Sebastian <cur...@earthlink.net> wrote in message
>news:0st67t4bhmkovkkgb...@4ax.com...
>> On Sat, 27 Jan 2001 18:28:11 -0700, Igor Kezhis <igo...@hotmail.com>
>> wrote:
>>
>> >When I try to serialize bool type I get error error "C2679: binary '>>'
>> >: no operator defined which takes a right-hand operand of type 'bool'
>> >(or there is no acceptable conversion)"
>
>I suspect you're leaving out some context.
>
>>
>> My guess is that MFC doesn't know anything about the bool data type.
>> Be sure to send a big thank-you to Microsoft for that.
>
>Why do I always get sucked into defending MS? :-)
>
>#include <iostream>
>#include <fstream>
[snip rest of program using Standard C++ i/o]
Hi Mike,
The missing context was in the subject of the post:
"CArchive and bool data type. How to serialize?"
I didn't say *VC* doesn't know about bool, I said *MFC* (of which
CArchive is a part) doesn't know about bool.
But that's what confusing to me. MFC is *written in C++*.
The MSVC++ translator does indeed support the native type
'bool'. There's no reason an MFC program can't use type
'bool', just as it can use 'int', etc.
The 'missing context' I speculated about was that perhaps
the >> operator in the error message was a member of some
class other than fstream et. al., for which member there
wasn't an overload that takes a 'bool' argument. IOW was
the offending line such as the third one below:
CArchive arch;
bool b;
arch << b;
In which case this is indeed an implementation-specific
issue.
Oh , but wait a moment. From above:
> error "C2679: binary '>>': no operator defined which takes
> a right-hand operand of type 'bool'
> (or there is no acceptable conversion)"
Perhaps it's as simple an error as writing >> when it
should have been <<. But I'm not familiar with the
CArchive class, so I won't speculate further. :-)
-Mike
You can write code which uses both MFC and bool. You can also
write code which uses both MFC and Corba. MFC doesn't support
bool any more than it supports Corba. It might not conflict
with it, but that's a far cry from supporting it.
VC++ supports bool in recent versions. No released version of
MFC has support for bool, AFAIK.
-- James Dennett.
It works when I write to the file etc. "ar<<bSomething". This line does not
gives any errors.
But when I try to read a file for example " ar>>bSomething " that when I get
that error.
When I looked in help files, it does not list bool as one of the types that can
be serialized, but that is very strange, because bool is just as important as
int or double, and that is line from MSDN:
"The overloaded extraction (>>) and insertion (<<) operators are convenient
archive programming interfaces that support both primitive types and
CObject-derived classes"
>
>
>It works when I write to the file etc. "ar<<bSomething". This line does not
>gives any errors.
It works on output because it converts the bool to an int. You might
want to make that explicit:
bool b;
ar << static_cast<int>(b);
>But when I try to read a file for example " ar>>bSomething " that when I get
>that error.
Read it into an int, then convert the int to a bool. That should give
you the results you want.
int x;
bool b;
ar >> x;
b = static_cast<bool>(x);
If conserving space in the file is vital, use char instead of int. If
you do that, you'll *have* to use the cast in the output part.
>When I looked in help files, it does not list bool as one of the types that can
>be serialized, but that is very strange, because bool is just as important as
>int or double, and that is line from MSDN:
>
>"The overloaded extraction (>>) and insertion (<<) operators are convenient
>archive programming interfaces that support both primitive types and
>CObject-derived classes"
That part of the the help file was likely written before bool was a
primitive type.
What I did is I declared BYTE variable, and then read data into it.
then based on what was in it, I set my bool variable.
Seems to be working.
Thanks.
>I have fixed it. Thanks.
>
>What I did is I declared BYTE variable, and then read data into it.
>then based on what was in it, I set my bool variable.
>
>Seems to be working.
DO THE CAST ON OUTPUT!
BYTE is just a typedef for char. If you aren't casting the output,
then if CArchive writes in binary mode (I think it does), you could
have subtle problems.
>Declaring char and then casting worked, but int did not. So I will go with char.
>If you can, could you explane what the problem would be if I use BYTE? Because everything seems to be
>working good. I can read, write and then read again.
>CArchive does writes in binary mode.
>Thanks
I didn't mean to say that BYTE was a problem (BYTE is, like I said,
just a typedef for char anyway). It's just that if you use it, you
should cast the output to a BYTE. It's been a while since I wrote an
MFC serialize function, but IIRC, the following uses the general form:
if(doing output)
{
// ...
ar << static_cast<BYTE>(m_boolval);
// ...
}
else
{
// ...
BYTE b;
ar >> b;
m_boolval = static_cast<bool>(b);
// ...
}
You could use any integral type in place of BYTE. The important thing
is that whatever you use as the "temporary" on input is the type you
cast to on output.