Thread View: alt.comp.lang.learn.c-c++
3 messages
3 total messages
Started by Maya
Mon, 22 Dec 2003 01:32
std streams and enum?
Author: Maya
Date: Mon, 22 Dec 2003 01:32
Date: Mon, 22 Dec 2003 01:32
51 lines
3246 bytes
3246 bytes
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> >&
>> jme::account_t&' operator <<<<<<<====== This is the problem *****
...: candidates are:
/* Gives me a list of other candidates */
====================================================
Thanks in advance
--
FYI
http://www.astro.umd.edu/~marshall/abbrev.html
E-Mail Policy
http://www.vif.com/users/escalante/Email_Policy.html
* You cannot exercise your power to a point of humiliation.
- 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: std streams and enum?
Author: Ulrich Eckhardt
Date: Sun, 21 Dec 2003 23:42
Date: Sun, 21 Dec 2003 23:42
33 lines
2002 bytes
2002 bytes
Maya wrote:
> 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: http://parashift.com/c++-faq-lite/ first !
Re: std streams and enum?
Author: Chris Newton
Date: Mon, 22 Dec 2003 03:02
Date: Mon, 22 Dec 2003 03:02
34 lines
3169 bytes
3169 bytes
Maya wrote... > 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads