|
-
Sep 23rd, 2002, 07:11 PM
#1
Thread Starter
New Member
simple overload of istream << help
hey guys i need help tonight. Im trying to overload istream<< for the enum Color. The error i get is an "illegal operand" for the input << temp; in the overload. Heres what i have...
#include <iostream>
istream & operator>>(istream& input, Color& hue);
int main(void)
{
cout << "enter in a color: Red, Blue -> ";
Color hue;
cin >> hue;
}
istream & operator>>(istream& input, Color& hue)
{
char temp[4];
input << temp;
if (temp == "Red ")
{
hue = Red;
}
else if (temp == "Blue")
{
hue = Blue;
}
else
cerr << "That is an illegal color";
return input;
}
-
Sep 24th, 2002, 05:47 AM
#2
Multiple errors.
a) istream & operator>>(istream& input, Color& hue);
istream is an unknown identifier at the global level, it is declared in the namespace std.
Add a
using namespace std;
directly after including the header.
b) input << temp;
wrong direction. Should be
input >> temp;
And temp must be at least 5 chars long as "Blue" takes 4 charcters + the NUL = 5.
Also it's unsafe because it's very easy to overflow: if the user enters more than 4 characters your app will crash!
c) You can't compare C-style strings using ==.
Either use the strcmp function which returns 0 if the two strings are the same or better yet, use the string class from the header <string>, which allows you to use the == operator.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|