Search code examples
c++visual-studioheader-filesiostream

istream compile error in C++ using Visual Studio


I’m using Visual Studio and I'm reacquainting myself with C++ before taking a few classes.

This is an old script that used to compile, but now istream is giving me a few syntax errors. If anyone can lead me in the right direction, that would be great.

error1 related to istream operator

error2 related to istream operator

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 Error Summary

vs brief error summary

Terminal Error Bottom Small Portion

/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.

Solution

  • Because it takes reference to CONST type. const Fraction& f, it must not be (just remove "const" in the both declaration and definition), the stream input operator is about to modify this object. Instead, const should be used in the stream output operator (e.g. std::ostream& operator<<(std::ostream& os, const Fraction& f)), where outputted object is not supposed to be modified.