使用 Visual Studio 的 C++ 中的 istream 编译错误
istream compile error in C++ using Visual Studio
我正在使用 Visual Studio,在学习一些 类 之前,我正在重新熟悉 C++。
这是一个用于编译的旧脚本,但现在 istream 给我一些语法错误。如果有人能引导我朝着正确的方向前进,那就太好了。
Fraction.h
friend istream& operator >> (istream& in, const Fraction& f);
Fraction.cpp
istream& operator >> (istream& in, const Fraction& f) {
cout << "Enter numerator" << endl;
in >> f.num;// error line
cout << "Enter denominator" << endl;
in >> f.denom; // error line
return in;
}
Visual Studio 错误摘要
终端错误底部小部分
/usr/include/c++/8/istream:803:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘unsigned char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:808:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)’
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^~~~~~~~
/usr/include/c++/8/istream:808:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘signed char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:980:5: note: candidate: ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&)’
operator>>(_Istream&& __is, _Tp&& __x)
^~~~~~~~
/usr/include/c++/8/istream:980:5: note: template argument deduction/substitution failed:
/usr/include/c++/8/istream: In substitution of ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_istream<char>&; _Tp = const int&]’:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: required from here
/usr/include/c++/8/istream:980:5: error: no type named ‘type’ in ‘struct std::enable_if<false, std::basic_istream<char>&>’
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
因为它引用了 CONST 类型。 const Fraction& f
,一定不是(只要去掉声明和定义中的“const”),流输入操作符即将修改这个对象。相反,应该在流输出运算符(例如 std::ostream& operator<<(std::ostream& os, const Fraction& f)
)中使用 const,其中不应修改输出对象。
我正在使用 Visual Studio,在学习一些 类 之前,我正在重新熟悉 C++。
这是一个用于编译的旧脚本,但现在 istream 给我一些语法错误。如果有人能引导我朝着正确的方向前进,那就太好了。
Fraction.h
friend istream& operator >> (istream& in, const Fraction& f);
Fraction.cpp
istream& operator >> (istream& in, const Fraction& f) {
cout << "Enter numerator" << endl;
in >> f.num;// error line
cout << "Enter denominator" << endl;
in >> f.denom; // error line
return in;
}
Visual Studio 错误摘要
终端错误底部小部分
/usr/include/c++/8/istream:803:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘unsigned char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:808:5: note: candidate: ‘template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(std::basic_istream<char, _Traits>&, signed char*)’
operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
^~~~~~~~
/usr/include/c++/8/istream:808:5: note: template argument deduction/substitution failed:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: note: cannot convert ‘f.Fraction::denom’ (type ‘const int’) to type ‘signed char*’
in >> f.denom;
~~^~~~~
In file included from /usr/include/c++/8/iostream:40,
from /home/spamandsons/projects/laney/Fraction.cpp:2:
/usr/include/c++/8/istream:980:5: note: candidate: ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&)’
operator>>(_Istream&& __is, _Tp&& __x)
^~~~~~~~
/usr/include/c++/8/istream:980:5: note: template argument deduction/substitution failed:
/usr/include/c++/8/istream: In substitution of ‘template<class _Istream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_istream<_Istream>, std::__is_extractable<typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type, _Tp&&, void> >::value, typename std::__is_convertible_to_basic_istream<_Tp>::__istream_type>::type std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_istream<char>&; _Tp = const int&]’:
/home/spamandsons/projects/laney/Fraction.cpp:188:13: required from here
/usr/include/c++/8/istream:980:5: error: no type named ‘type’ in ‘struct std::enable_if<false, std::basic_istream<char>&>’
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
因为它引用了 CONST 类型。 const Fraction& f
,一定不是(只要去掉声明和定义中的“const”),流输入操作符即将修改这个对象。相反,应该在流输出运算符(例如 std::ostream& operator<<(std::ostream& os, const Fraction& f)
)中使用 const,其中不应修改输出对象。