473,382 Members | 1,775 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,382 software developers and data experts.

Problem overloading extraction operator

Working on Visual Studio .Net I've implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor's name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;
cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}

But I keep getting this error: error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Gender' (or there is
no acceptable conversion)

I've got gender defined in my constructors as well as the overloading
implementation. I don't understand why I'm getting this error. Can
someone help?

Many thanks.

May 6 '07 #1
16 4310
EM********@gmail.com wrote:
Working on Visual Studio .Net I've implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
Do *NOT* put using namespace std; into a header file.
>
class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor's name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;
Your error is here.
cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}

But I keep getting this error: error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Gender' (or there is
no acceptable conversion)

I've got gender defined in my constructors as well as the overloading
implementation. I don't understand why I'm getting this error. Can
someone help?
Your compiler is telling you exactly what is wrong. As part of
operator>>(istream&,Contributor&), you are using the extraction operator
on an object of type Gender (specifically, RHS.MF). The extraction
operator is not defined for that enumeration.

Define it (and the insertion operator), and all will be well.
May 6 '07 #2
* EM********@gmail.com:
Working on Visual Studio .Net I've implemented a class:

#ifndef CONTRIBUTOR_H
#define CONTRIBUTOR_H

enum Gender {male=1, female, unk};
OK so far.

#include <iostream>
#include <iomanip>
Instead of these heavy-weight headers, consider using the light-weight
<iosfwdin a header file.

#include <string>
using namespace std;
Never have "using namespace std" in a header file.

class Contributor
{
friend ostream& operator << (ostream& output, Contributor& RHS);
friend istream& operator >(istream& input, Contributor& RHS);
Reserve all uppercase for macros. See this group's FAQ as well as
Bjarne Stroustrup's FAQs.

public:
Contributor();
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that. Unless the
sexual organs really matter for the contribution (whatever it is). If,
for example, the gender information is used to establish a proper
honorific in a computer generated thank-you letter, it would be much
better design-wise to store the proper honorific. It would also yield
simpler code; see below.

Contributor(const Contributor &copy);
Contributor& operator=(const Contributor& RHS);
bool operator== (const Contributor& RHS);
~Contributor() {cout << "In Contributor Destructor" << endl;}

private:
std::string Name;
double Contribution;
Gender MF;
int IDKey;
};
Note: with the data members you have there's no need to define a copy
constructor and assignment operator; the compiler generated ones will do
nicely.

Then I tried to overload the extraction operator thus:

istream& operator >(istream& input, Contributor& RHS)
{
cout << "\nEnter a new contributor's name: ";
std::getline (cin, RHS.Name);
cout << "Enter the amount of their contribution: ";
cin >RHS.Contribution;
cout << "What is the gender of the contributor? ";
cin >RHS.MF;
cout << "Assign an ID Key to the contributor: ";
cin >RHS.IDKey;

return input;
}
Consider how you would test your class with prepared data in a file.
Consider how you'd use it in a batch job (silent, no user interaction).

But I keep getting this error: error C2679: binary '>>' : no operator
found which takes a right-hand operand of type 'Gender' (or there is
no acceptable conversion)

I've got gender defined in my constructors as well as the overloading
implementation. I don't understand why I'm getting this error. Can
someone help?
Gender is defined as an enum type. You can provide an extractor for it.
That function (example below) /defines/, implicitly, the textual
representation of genders, here as simple decimal integers:

istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

Note that this would be very much easier it you stored a preferred
honorific (string) instead of a gender (enum).

Hth.,

- Alf

--
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?
May 6 '07 #3
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:

Contributor(string Name, double Contribution, Gender MF, int IDKey);

Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
This code will allow values to be assigned to 'g' that are not valid
members of the enum.

May 7 '07 #4
* Old Wolf:
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
>* EM.Bate...@gmail.com:
>> Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.

I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
The paragraph you've quoted the start of is simply about choosing a bad
option both designwise and codingwise, versus choosing something better:
no political correctness issue, and no ranting.

Please read before you write.

> istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

This code will allow values to be assigned to 'g' that are not valid
members of the enum.
Please read before you write.

--
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?
May 7 '07 #5
On May 7, 12:15 pm, "Alf P. Steinbach" <a...@start.nowrote:
* Old Wolf:
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.

The paragraph you've quoted the start of is simply about choosing a bad
option both designwise and codingwise, versus choosing something better:
no political correctness issue, and no ranting.

Please read before you write.
The issue is whether it matters what sex a contributor is.
This is nothing to do with coding. Code-design decisions
shouldn't be influenced by /minor/ coding considerations.

In fact this is not even an issue of code design. It is an
issue of what the program is supposed to actually do,
which should almost never be influenced by coding
considerations. Who are you to say that what is or is
not a relevant piece of information for this project?

You have no idea what the OP's situation is, or what he is
intending to do with this information. You hazard a guess
with no rationale and then criticise the OP's code for not
being 'better' and 'easier' than your guess (never mind what
those words are supposed to mean).

In case I need to spell it out: it is better to have more lines
of code to achieve the specified objective, than a short clear
program which does the wrong thing.

May 7 '07 #6
* Old Wolf:
On May 7, 12:15 pm, "Alf P. Steinbach" <a...@start.nowrote:
>* Old Wolf:
>>On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
The paragraph you've quoted the start of is simply about choosing a bad
option both designwise and codingwise, versus choosing something better:
no political correctness issue, and no ranting.

Please read before you write.

The issue is whether it matters what sex a contributor is.
This is nothing to do with coding. Code-design decisions
shouldn't be influenced by /minor/ coding considerations.

In fact this is not even an issue of code design. It is an
issue of what the program is supposed to actually do,
which should almost never be influenced by coding
considerations. Who are you to say that what is or is
not a relevant piece of information for this project?

You have no idea what the OP's situation is, or what he is
intending to do with this information. You hazard a guess
with no rationale and then criticise the OP's code for not
being 'better' and 'easier' than your guess (never mind what
those words are supposed to mean).

In case I need to spell it out: it is better to have more lines
of code to achieve the specified objective, than a short clear
program which does the wrong thing.
Well, again I can only ask you to please read before you write.

Please read before you write.

No absolute judgement about that issue was communicated by me. The very
next sentence in the paragraph you quoted a little bit of, stated the
conditions under which the advice would be valid. You're trolling.

--
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?
May 7 '07 #7
On May 6, 6:03 pm, Old Wolf <oldw...@inspire.net.nzwrote:
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.

I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

This code will allow values to be assigned to 'g' that are not valid
members of the enum.
I'm not very skilled at C++. Should this be set as a separate
function in my .cpp file or should it go inside the current extraction
overloading code?

May 7 '07 #8
On May 6, 8:39 pm, "EM.Bate...@gmail.com" <EM.Bate...@gmail.com>
wrote:
On May 6, 6:03 pm, Old Wolf <oldw...@inspire.net.nzwrote:


On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
This code will allow values to be assigned to 'g' that are not valid
members of the enum.

I'm not very skilled at C++. Should this be set as a separate
function in my .cpp file or should it go inside the current extraction
overloading code?- Hide quoted text -

- Show quoted text -
This is an exercise for my Data Structures class in college. I'm
marginal at C++ coding, at best. I didn't design the class or the
program, I just have to implement it. I guess I'm over my head.

May 7 '07 #9
* EM********@gmail.com:
>On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
>> istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}

I'm not very skilled at C++. Should this be set as a separate
function in my .cpp file or should it go inside the current extraction
overloading code?
A declaration of the function belongs with the enum. The most natural
given the code packaging you've already chosen is to place the
definition in the implementation file. You'd do well to replace the
comments about possible code, with actual code: either throwing an
exception, or communicating an input error via the stream error state.

--
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?
May 7 '07 #10
On May 7, 1:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Gender is defined as an enum type. You can provide an extractor for it.
That function (example below) /defines/, implicitly, the textual
representation of genders, here as simple decimal integers:
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
This violates the normal contract of operator>>, which is to
signal errors by means of the failbit (or the badbit, for hard
IO errors).
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
The range check is essential if you want to avoid undefined
behavior. It should set failbit, however, and not throw an
exception.
return input;
}
A more reasonable implementation would use a single character,
'M' or 'F', to choose (with maybe '-' for unknown). The same
principle applies, however: input into a variable (of type
char), then:

switch ( value ) {
case 'M' :
case 'm' :
g = male ;
break ;

case 'F' :
case 'f' :
g = female ;
break ;

case '-' :
g = unk ;
break ;

default :
input.setstate( std::ios::failbit ) ;
break ;
}

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 7 '07 #11
On May 7, 2:15 am, "Alf P. Steinbach" <a...@start.nowrote:
* Old Wolf:
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
The paragraph you've quoted the start of is simply about choosing a bad
option both designwise and codingwise, versus choosing something better:
no political correctness issue, and no ranting.
The problem is that the original poster didn't give any
information as to what the field might be used for. There was
no reason to suppose that it was used to generate the honorific
(which, as you point out, should be a separate string). A lot
of contexts still require maintaining such information, by law,
and others will do so for statistical purposes.
Please read before you write.
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
This code will allow values to be assigned to 'g' that are not valid
members of the enum.
Please read before you write.
You mention the fact in comments, but you fail to point out that
doing so is in fact undefined behavior, which means that such a
test really isn't optional. (On the other hand, throwing an
exception in case of failure is NOT correct behavior for an
extractor.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 7 '07 #12
On May 7, 4:39 am, "EM.Bate...@gmail.com" <EM.Bate...@gmail.com>
wrote:
On May 6, 6:03 pm, Old Wolf <oldw...@inspire.net.nzwrote:
On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
This code will allow values to be assigned to 'g' that are not valid
members of the enum.
I'm not very skilled at C++. Should this be set as a separate
function in my .cpp file or should it go inside the current extraction
overloading code?
It depends. Given that you've defined Gender outside the class,
making it publicly available, I'd declare the function in a
header, and implement it in a source file somewhere. Depending
on the use of the class, however, it might be valid to define
the type in class, and implement the input function directly
inline.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 7 '07 #13
* James Kanze:
On May 7, 1:08 am, "Alf P. Steinbach" <a...@start.nowrote:
>* EM.Bate...@gmail.com:
>Gender is defined as an enum type. You can provide an extractor for it.
That function (example below) /defines/, implicitly, the textual
representation of genders, here as simple decimal integers:
> istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }

This violates the normal contract of operator>>, which is to
signal errors by means of the failbit (or the badbit, for hard
IO errors).
Not really. First, streams can be set to throw exceptions: you can
choose between two contracts, and the choice is yours. The only problem
with that is that the contract which isn't your "normal" one is just as
inherently flawed as is your "normal" one, and perhaps a little worse
(as is most everything with iostreams, except for trivial toy program
usage). Second, in any application there may be and quite likely is a
contractual regime above the level of direct calls to standard library
functions, where stream errors are converted to exceptions (in a far
more reliable way than streams can do themselves), and it makes just as
much sense to do that directly than to provide a higher level wrapper.

> // Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );

The range check is essential if you want to avoid undefined
behavior.
Not really. :-) It all depends on what the function's contract is: what
it requires and what results it guarantees in return. I happen to agree
with you that it's best to have the check in place, not the least
because without it there's a likely entry vector for /badware/, but e.g.
Betrand Meyer has argued extremely forcefully the opposite viewpoint.

It should set failbit, however, and not throw an
exception.
There I definitely disagree with you (see above, plus, adding work to
conform to a harebrained scheme like that particular contract, of the
two possible ones, is IMHO just silly), but again, there are people --
I don't recollect names and am too lazy to Google -- who have argued
very forcefully your point of view here, which isn't difficult since the
natural assumption of readers is that as part of the standard library,
there must be something worthwhile in the design of iostreams.

>
> return input;
}

A more reasonable implementation would use a single character,
'M' or 'F', to choose (with maybe '-' for unknown). The same
principle applies, however: input into a variable (of type
char), then:

switch ( value ) {
case 'M' :
case 'm' :
g = male ;
break ;

case 'F' :
case 'f' :
g = female ;
break ;

case '-' :
g = unk ;
break ;

default :
input.setstate( std::ios::failbit ) ;
break ;
}
Well yes, agreed, but my simple example was less to type. ;-)

--
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?
May 7 '07 #14
* James Kanze:
On May 7, 2:15 am, "Alf P. Steinbach" <a...@start.nowrote:
>* Old Wolf:
>>On May 7, 11:08 am, "Alf P. Steinbach" <a...@start.nowrote:
* EM.Bate...@gmail.com:
Contributor(string Name, double Contribution, Gender MF, int IDKey);
Unrelated to C++ but, what does it matter whether a Contributor has a
penis, a vagina, or perhaps nothing? I'd remove that.
>>I for one, would appreciate not seeing political correctness
ranting here. I get enough of it in the state-sponsored media.
>The paragraph you've quoted the start of is simply about choosing a bad
option both designwise and codingwise, versus choosing something better:
no political correctness issue, and no ranting.

The problem is that the original poster didn't give any
information as to what the field might be used for. There was
no reason to suppose that it was used to generate the honorific
(which, as you point out, should be a separate string). A lot
of contexts still require maintaining such information, by law,
and others will do so for statistical purposes.
In private communication he's informed me it was a requirement of a
college assignment. Anyway, it's not a problem. The advice was
properly qualified. I'd have thrown in weasel words too if I'd known
people would get so riled up about it. It was good advice, but note
that the quoting snipped the most significant parts.

>Please read before you write.
>>> istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
>>This code will allow values to be assigned to 'g' that are not valid
members of the enum.
>Please read before you write.

You mention the fact in comments, but you fail to point out that
doing so is in fact undefined behavior
It isn't undefined behavior, although it can be. As an analogy, x*y
isn't undefined behavior, although it can be, depending on what's known
about x and y. Adding checks everywhere there's a /possibility/ of UB
when you remove all knowledge about execution context, means adding
checks around every basic operation, and that's just silly.

>, which means that such a
test really isn't optional.
No.

(On the other hand, throwing an
exception in case of failure is NOT correct behavior for an
extractor.)
It can be correct, it can be incorrect, it all depends on context, just
like the possible UB of multiplication. It's the simplest thing to do.
And it's far more reliable than setting badbit.

--
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?
May 7 '07 #15
* Alf P. Steinbach:
* James Kanze:
>On May 7, 2:15 am, "Alf P. Steinbach" <a...@start.nowrote:
>>>> istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
>>>[Old Wolf] This code will allow values to be assigned to 'g' that are not valid
members of the enum.
>>Please read before you write.

You mention the fact in comments, but you fail to point out that
doing so is in fact undefined behavior

It isn't undefined behavior, although it can be. As an analogy, x*y
isn't undefined behavior, although it can be, depending on what's known
about x and y. Adding checks everywhere there's a /possibility/ of UB
when you remove all knowledge about execution context, means adding
checks around every basic operation, and that's just silly.
Sorry, on second thoughts (1) disregarding the built-in UB of iostreams
here, not checking can't be UB (it can at worst be unspecified result,
§5.2.9/7, which means it depends on the implementation, §1.3.13) and (2)
the function is therefore potentially more useful without the check or
with the check optional (one could always define a checking wrapper).
Somehow I instinctively did The Right Thing, but your comment about UB
confused me when defending that. It's just that I'm used to you being
Always Right about basic facts, and so I didn't think.

--
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?
May 7 '07 #16
On May 7, 1:41 am, "Alf P. Steinbach" <a...@start.nowrote:
* Alf P. Steinbach:


* James Kanze:
On May 7, 2:15 am, "Alf P. Steinbach" <a...@start.nowrote:
istream& operator>>( istream& input, Gender& g )
{
int genderVal;
input >genderVal;
if( input.fail() ) { throw std::runtime_error( "Ouch ouch" ); }
// Here perhaps check the value and throw exception if not OK.
// Assuming it is OK:
g = static_cast<Gender>( genderVal );
return input;
}
>>[Old Wolf] This code will allow values to be assigned to 'g' that are not valid
members of the enum.
>Please read before you write.
You mention the fact in comments, but you fail to point out that
doing so is in fact undefined behavior
It isn't undefined behavior, although it can be. As an analogy, x*y
isn't undefined behavior, although it can be, depending on what's known
about x and y. Adding checks everywhere there's a /possibility/ of UB
when you remove all knowledge about execution context, means adding
checks around every basic operation, and that's just silly.

Sorry, on second thoughts (1) disregarding the built-in UB of iostreams
here, not checking can't be UB (it can at worst be unspecified result,
§5.2.9/7, which means it depends on the implementation, §1.3.13) and (2)
the function is therefore potentially more useful without the check or
with the check optional (one could always define a checking wrapper).
Somehow I instinctively did The Right Thing, but your comment about UB
confused me when defending that. It's just that I'm used to you being
Always Right about basic facts, and so I didn't think.

--
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?- Hide quoted text -

- Show quoted text -
OK, you guys have just lost me now. I've got it working but I have no
clue whether it's right or not. Here I quote the requirements of the
lab:
Data members:
std::string Name; //the name of the contributor - use the
Standard Library string class as the type
double Contribution //the amount of the contribution given
to the cause
enum Gender {male, female, none}; //the gender of the
contributor
int IDKey; //identifier key for the Hashing list if desired
Member Functions/Methods:
default Constructor and overloaded Constructor accepting a
string for the name and the appropriate other information
Copy Constructor
Destructor
Overloaded << (insertion operator for your Contributor
objects) to show the object
Overloaded >(extraction operator for your Contributor
objects) to load the object
Overloaded assignment (=)
Overloaded <, >, ==, != (return a C++ bool type - use the
contributor's name to determine order, use all member data to
determine equality or non-equality)

Going forward we will be using this class to build a linked list
(singularly and doubly-linked as well as circular), a stack, a queue
and a binary tree.

Does this help?
May 7 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Frédéric Manzanares | last post by:
hello, my problem: I want to habe one Class with write and read in a file. i have overloaded the operator >> and <<. class c_File { public : fstream fs;
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
16
by: gorda | last post by:
Hello, I am playing around with operator overloading and inheritence, specifically overloading the + operator in the base class and its derived class. The structure is simple: the base class...
2
by: ceo | last post by:
Hi there, I'm trying to overload insertion (<<) and extraction (>>) operators and the program yields output I didn't expect. Could someone please clarify what's wrong with my program. Thanks,...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.