Board index » cppbuilder » Re: std streams and enum?

Re: std streams and enum?


2003-12-22 07:42:15 AM
cppbuilder68
Maya wrote:
Quote
enum account_t {Zero, One, Two};
std::istream&
jme::operator>>(std::istream& is, jme::Account& obj){
is>>obj.type; // <<======== Here is the problem ****
return is;
}
account.H: In function `std::istream& jme::operator>>(std::istream&,
jme::Account&)':
account.H:91: no match for `std::basic_istream<char,
std::char_traits<char>>&
>>jme::account_t&' operator <<<<<<<====== This is the problem *****
...: candidates are:
Well, how should IOStreams know about your enumeration and how it is
supposed to be written to a stream? One simple solution is to overload the
operator for it, another is to just use a temporary int and a
static_cast<>.
One more note, you can nest enumerations inside classes/structs:
class Account
{
enum Type
{ one, two, three };
};
The fully qualified type is then Account::Type (just like with namespaces).
Uli
--
Questions ?
see C++-FAQ Lite: parashift.com/c++-faq-lite/ first !
 
 

Re:Re: std streams and enum?

Can someone please help me?
I am using an enum type that I want to store in a std::IOStream object,
but the compiler [complainer] tells me that the format is not supported.
enum types are the same as 'const int' right? So, there should not be a
problem!
namespace jme{
enum account_t {Zero, One, Two};
class Account{
protected:
account_t type;
....
public:
friend std::istream&
jme::operator>>(std::istream& is, jme::Account& obj){
is>>obj.type; // <<======== Here is the problem ****
return is;
}
};
}
This gives me an error that reads:
====================================================
account.H: In function `std::istream& jme::operator>>(std::istream&,
jme::Account&)':
account.H:91: no match for `std::basic_istream<char,
std::char_traits<char>>&
Quote
>jme::account_t&' operator <<<<<<<====== This is the problem *****
...: candidates are:
/* Gives me a list of other candidates */
====================================================
Thanks in advance
--
FYI
www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of {*word*164}.
- Jean Chretien
* The media's the most powerful entity on earth. They have the
power to make the innocent guilty and to make the guilty
innocent, and that's power.
- Malcom X
* "Innocent until proven guilty", no... not in Canada!!
 

Re:Re: std streams and enum?

Maya wrote...
Quote
I am using an enum type that I want to store in a std::IOStream object,
but the compiler [complainer] tells me that the format is not supported.
enum types are the same as 'const int' right?
No, enumerations are types in their own right. You can implicitly
convert from an enumerator into its associated numeric value (which will
have an integral type, though it may or may not be int). However, you
*cannot* implicitly convert from an integer value to an enumerator,
which is what your example was effectively trying to do.
Instead, the easiest way to proceed is to read the value into a suitable
integral type, and then cast to the enumeration type, after performing
whatever sanity checks are appropriate on the value.
A better but more advanced technique is to provide a specific operator>>
to read from an istream into the enumeration type, which performs the
sanity checks itself and then either sets the enumeration value or sets
appropriate failure flags on the stream. This approach is much easier to
read and more robust, particularly if you do the smart thing and provide
a matching operator<< to output the enumeration type to an ostream.
Incidentally, you should be aware that outputting the enumerators as a
numerical value is a bit of a future-proofing hazard. If you ever
reorder the enumerators in the definition of the enumeration type, for
example by inserting a new value somewhere in the middle, your code will
still compile and run fine, but you'll break backward compatibility with
any existing data you've written out. For this reason, you might
consider it safer to have your << and>>operators store the enumerators
as well-defined strings, independently of their numerical values, rather
than just casting to/from an integer.
HTH,
Chris
 

{smallsort}