tôi thấy C++ cung cấp các chức năng mặc định operator<<
cho enum
loại:Tại sao C++ không cung cấp chức năng "operator >>" mặc định cho kiểu enum?
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cout << t;
return 0;
}
kết quả Chạy là:
0
Trong khi không cung cấp mặc định operator>>
chức năng:
#include <iostream>
using namespace std;
enum OpType {
Select,
Insert
};
int main() {
OpType t = Select;
cin >> t;
return 0;
}
Build nó sẽ tạo ra các lỗi biên dịch sau:
prog.cpp: In function ‘int main()’:
prog.cpp:11:6: error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘OpType’)
cin >> t;
~~~~^~~~
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:168:7: note: candidate: 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>] <near match>
operator>>(bool& __n)
^~~~~~~~
/usr/include/c++/6/istream:168:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:172:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator>>(short& __n);
^~~~~~~~
/usr/include/c++/6/istream:172:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short int&’ from an rvalue of type ‘short int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:175:7: note: candidate: 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>] <near match>
operator>>(unsigned short& __n)
^~~~~~~~
/usr/include/c++/6/istream:175:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘short unsigned int&’ from an rvalue of type ‘short unsigned int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:179:7: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char; _Traits = std::char_traits<char>] <near match>
operator>>(int& __n);
^~~~~~~~
/usr/include/c++/6/istream:179:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:182:7: note: candidate: 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>] <near match>
operator>>(unsigned int& __n)
^~~~~~~~
/usr/include/c++/6/istream:182:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘unsigned int&’ from an rvalue of type ‘unsigned int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:186:7: note: candidate: 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>] <near match>
operator>>(long& __n)
^~~~~~~~
/usr/include/c++/6/istream:186:7: note: conversion of argument 1 would be ill-formed:
prog.cpp:11:9: error: invalid initialization of non-const reference of type ‘long int&’ from an rvalue of type ‘long int’
cin >> t;
^
In file included from /usr/include/c++/6/iostream:40:0,
from prog.cpp:1:
/usr/include/c++/6/istream:190:7: note: candidate: 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>] <near match>
......
Tại sao C++ không cung cấp chức năng mặc định operator>>
cho loại enum?
chuyển đổi ngầm tồn tại từ 'enum E' thành' int', nhưng không phải giữa 'enum E &' và 'int &' –
Thực ra không có toán tử 'đặc biệt <<' nào cho liệt kê. Thay vào đó, việc liệt kê được chuyển thành một giá trị số nguyên, và giá trị số nguyên đó sau đó được sử dụng. –