Ferrous Moon
http://www.ferrousmoon.com:80/forums/

Need help with small C++ program: Returns! :P
http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=942
Page 1 of 1

Author:  eddieringle [Thu Aug 23, 2007 9:25 am ]
Post subject:  Need help with small C++ program: Returns! :P

Okay, I have been reading the tutorials at learncpp.com (which imo are the best tutorials yet, 4.8/5 stars).

I have been experimenting with enum types... well, just check out the code:

species.cpp:
Code:
#ifndef SPECIES_CPP #define SPECIES_CPP #include "Includes.h" #include "species.h" #endif int PickSpecies() { Species eSpecies; cout << "Which species would you like to be?" << endl; cout << "1. Human\n2. Dog\n3. Cat\n4. Bob\n Enter the number of your choice: "; cin >> eSpecies; switch (eSpecies) { case speciesHuman: cout << "You are a Human.\n"; break; case speciesDog: cout << "You are a Dog.\n"; break; case speciesCat: cout << "You are a Cat.\n"; break; case speciesBob: cout << "You are a Bob.\n"; break; default: cout << "You didn't enter a number..."; break; } return eSpecies; }
species.h:
Code:
#ifndef SPECIES_H #define SPECIES_H #include "Includes.h" #endif enum Species { speciesHuman = 1, speciesBob = 4, speciesDog = 2, speciesCat = 3 };
Here's my error:
species.cpp(14) : error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Species' (or there is no acceptable conversion)

Author:  frenchfrog [Thu Aug 23, 2007 10:03 am ]
Post subject: 

Since Species is not directly a int there is no >> operator defined.

Try?
Code:
int eSpecies;

Edit: other comments:

species.cpp: remove, .cpp files are compiled only once.
Code:
#ifndef SPECIES_CPP #define SPECIES_CPP ... #endif
species.h: Should look like
Code:
#ifndef SPECIES_H #define SPECIES_H #include "Includes.h" enum Species { speciesHuman = 1, speciesBob = 4, speciesDog = 2, speciesCat = 3 }; #endif
Also Includes.h is a strange name for a include file ;) (just style here)

Author:  eddieringle [Thu Aug 23, 2007 10:09 am ]
Post subject: 

Fixed.

Thanks.

But why is the >> any part of this? It's a part of io.

Author:  frenchfrog [Thu Aug 23, 2007 11:40 am ]
Post subject: 

Or I guess you could overload the >> operator for Species:
Code:
// Overload the >> operator for Species // istream is an input stream (could be a file, a network socket, console input (ie: cin), ...) // the reason this function return istream& is to able to chain up >> (ex: cin >> inta >> intb >> intc; ) // &is is the input stream on the left side of the operator (ex: cin >> inta; // is& would be cin) istream& operator >> (istream &is, Species &spe) { int intSpecie; is >> intSpecie; spe = static_cast<Species>(intSpecie); return is; }
And then keep your original code in PickSpecies.

Author:  eddieringle [Thu Aug 23, 2007 11:52 am ]
Post subject: 

This is my program so far (I was bored so I decided to do this, I've learned a couple things from it, I don't really think that there is a point to this program...)

http://www.e-ringtech.com/Tut1.exe


P.S. You guys should really either upgrade to phpBB3 or install an attachment mod...

Author:  frenchfrog [Thu Aug 23, 2007 6:46 pm ]
Post subject: 

.exe ? Plain source or nothing, really.

Author:  Lorehead [Thu Aug 23, 2007 8:18 pm ]
Post subject: 

I see two problems with this program as written. The first is that your value is not an integer; it is a Species. So, declare the function as returning a Species value, not an int. You likewise should not be treating the Species values as ints without casting them. An enum data type is an abstraction; the point of it is that it shouldn't ever hold values outside its range. Don't worry about how the compiler represents it internally.

The second is that the function doesn't check its input at all, so it can easily return garbage. At minimum, you'd need to check cin::fail() to see whether the user really typed in an integer, and then check the range to see whether the integer mapped to a valid species.

Page 1 of 1 All times are UTC-05:00
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/