我的代码中有一个枚举,它是这样的:enum Status {In-Active, Active};
,状态对象作为参数传递给Person
对象,所以我希望用户输入Active或In-Active,并将它们保存在Status变量中,我该怎么做呢?我最初尝试过,但没有成功。
下面是我的代码:
#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;
}
下面是我得到的错误(这是构建日志):
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
我的代码哪里出错了?谢谢
4条答案
按热度按时间dhxwm5r41#
你不能直接读取
enum
值,你需要一个std::map
来将用户输入Map到enum
值。hiz5n14c2#
枚举状态{InActive,Active};
地位状况;
cout〈〈“输入状态:“; cin〉〉status;
v7pvogib3#
你也可以将枚举封装在一个类中并提供操作符〉〉。
hujrc8aj4#
晕,我是新来的,但如果我的代码可以启发你: