c++ enum and using them to input from user

2019-03-19 07:01发布

问题:

I have an enum in my code that is the following: enum Status {In-Active, Active};. A status object is passed to a Person object as a parameter, so I want the user to enter Active or In-Active and save them in a Status variable. How can I do this? I tried it at first but it didnt work.

Here is my code:

#include <iostream>

using namespace std;

enum Status {InActive, Active};

class Person {
    private:
        string name;
        int age;
        Status status;

    public:
        Person (const string &_name, const int _age, const Status _status) : name(_name), age(_age), status(_status) {}

        string &getName () { return name; }
        int getAge () { return age; }
        Status getStatus () { return status; }

        void setName (const string &_name) { name = _name; }
        void setAge (const int _age) { age = _age; }
        void setStatus (const Status _status) { status = _status; }
};

////////////////////////////////////////////////////////////////////////////////
///////////////////////////////// Main Method //////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

int main () {
    string name;
    int age;
    Status status;

    cout << "Enter a name: "; getline(cin, name);
    cout << "Enter age: "; cin >> age;
    cout << "Enter status: "; cin >> status;

    Person p(name, age, status);

    return 0;
}

and here is the error I got (this is the build log):

Compiling: C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp: In function 'int main()':
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp:36:38: error: no match for 'operator>>' in 'std::cin >> status'
C:\Users\KiKo-SaMa\Desktop\C++\Person.cpp:36:38: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:122:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:122:7: note:   no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&) {aka std::basic_istream<char>& (*)(std::basic_istream<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:126:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>, std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:126:7: note:   no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:133:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:133:7: note:   no known conversion for argument 1 from 'Status' to 'std::ios_base& (*)(std::ios_base&)'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:169:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:169:7: note:   no known conversion for argument 1 from 'Status' to 'bool&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:173:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:173:7: note:   no known conversion for argument 1 from 'Status' to 'short int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:176:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:176:7: note:   no known conversion for argument 1 from 'Status' to 'short unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:180:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:180:7: note:   no known conversion for argument 1 from 'Status' to 'int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:183:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:183:7: note:   no known conversion for argument 1 from 'Status' to 'unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:187:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:187:7: note:   no known conversion for argument 1 from 'Status' to 'long int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:191:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:191:7: note:   no known conversion for argument 1 from 'Status' to 'long unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:196:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:196:7: note:   no known conversion for argument 1 from 'Status' to 'long long int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:200:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:200:7: note:   no known conversion for argument 1 from 'Status' to 'long long unsigned int&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:205:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:205:7: note:   no known conversion for argument 1 from 'Status' to 'float&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:209:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:209:7: note:   no known conversion for argument 1 from 'Status' to 'double&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:213:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:213:7: note:   no known conversion for argument 1 from 'Status' to 'long double&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:217:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:217:7: note:   no known conversion for argument 1 from 'Status' to 'void*&'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:241:7: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_istream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:241:7: note:   no known conversion for argument 1 from 'Status' to 'std::basic_istream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:998:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/istream.tcc:957:5: note: template<class _CharT2, class _Traits2> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT2*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/istream.tcc:925:5: note: template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, _CharT&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:709:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:714:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char&)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:756:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, unsigned char*)
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/istream:761:5: note: template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)
Process terminated with status 1 (0 minutes, 0 seconds)
43 errors, 0 warnings

where did my code go wrong? Thanks

回答1:

You can't read enum values directly, you'll need a std::map that maps user input to an enum value.

std::map<std::string,Status> m;
m["In-Active"] = In-Active;
m["Active"] = Active;

std::string sstatus;
cin >> sstatus;
Status status = m[sstatus];


回答2:

  • Enum objects are being parsed at compile time and it can only contain constant integer values.

  • Person p(name, age, status); //Here without using status object you should send a value directly like "In-Active" or "Active". So there would be no input from the user regarding th status field.

enum Status {InActive, Active};

  • Here Status is a enum class and any object created would contain values "Inactive" or "Active". If you try to assign some other value you would get an compiler error.

Status status;

  • Here status is an enum object created and it contains garbage value.

cout << "Enter status: "; cin >> status;

  • Here you are asking user for an input for an enum object which is totally invalid for the compiler.


回答3:

You can also wrap the enum inside a class and provide operator>>. This is a modified example from this link http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Type_Safe_Enum

#include <iostream>

template<typename def>
class safe_enum : public def
{
public:
  int val;
  safe_enum(int v) : val(v) {}
//  inner underlying() const { return val; }
  bool operator == (const safe_enum & s) const { return this->val == s.val; }
  bool operator != (const safe_enum & s) const { return this->val != s.val; }
  bool operator <  (const safe_enum & s) const { return this->val <  s.val; }
  bool operator <= (const safe_enum & s) const { return this->val <= s.val; }
  bool operator >  (const safe_enum & s) const { return this->val >  s.val; }
  bool operator >= (const safe_enum & s) const { return this->val >= s.val; }
};

struct status_def {
  enum type { NoValue, InActive, Active };
};
typedef safe_enum<status_def> Status;

std::istream& operator>>(std::istream& Istr, Status& other)
{
    std::cout << "Enter Status value: " << std::endl; 
    //here the logic to handle input error
    Istr >> other.val;
    return Istr;
}

std::ostream& operator<<(std::ostream& os,const Status& toPrint)
{
    if (toPrint.val == Status::Active)
        return os << "Active";
    if (toPrint.val == Status::NoValue)
        return os << "NoValue";
    if (toPrint.val == Status::InActive)
        return os << "InActive";
    throw;
}

int main(void)
{
    Status test(Status::NoValue);
    std::cout << test << std::endl;
    std::cin >> test;
    std::cout << test << std::endl;
}


标签: c++ input enums