c++ - C2679 compiler error constructing const int member variable from std::istream -
why can not construct integer member variable istream in code below? there way?
#include <iostream> // std::cout #include <fstream> // std::ifstream class { public: a(std::istream& strm) : n(strm >> n) {} // error on line*** void get_number(std::istream& strm) { int a; strm >> a; std::cout << "you retrieved: " << << " stream\n"; } private: const int n; }; int main() { std::ifstream ifs ("test.txt", std::ifstream::in); a(ifs); // works int n; ifs >> n; std::cout << n; return 0; }
here error message vs2008:
1>------ build started: project: stream_test, configuration: debug win32 ------ 1>compiling... 1>main.cpp 1>c:\cpp\stream_test\stream_test\main.cpp(7) : error c2679: binary '>>' : no operator found takes right-hand operand of type 'const int' (or there no acceptable conversion) 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(1144): 'std::basic_istream<_elem,_traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_elem,_traits> &,signed char *)' [found using argument-dependent lookup] 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(1146): or 'std::basic_istream<_elem,_traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_elem,_traits> &,signed char &)' [found using argument-dependent lookup] 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(1148): or 'std::basic_istream<_elem,_traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_elem,_traits> &,unsigned char *)' [found using argument-dependent lookup] 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(1150): or 'std::basic_istream<_elem,_traits> &std::operator >><std::char_traits<char>>(std::basic_istream<_elem,_traits> &,unsigned char &)' [found using argument-dependent lookup] 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(155): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(std::basic_istream<_elem,_traits> &(__cdecl *)(std::basic_istream<_elem,_traits> &))' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(161): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(std::basic_ios<_elem,_traits> &(__cdecl *)(std::basic_ios<_elem,_traits> &))' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(168): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(175): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(std::_bool &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(194): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(short &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(228): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(unsigned short &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(247): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(int &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(273): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(unsigned int &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(291): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(long &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(309): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(__w64 unsigned long &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(329): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(__int64 &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(348): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(unsigned __int64 &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(367): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(float &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(386): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(double &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(404): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(long double &)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(422): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(void *&)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\istream(441): or 'std::basic_istream<_elem,_traits> &std::basic_istream<_elem,_traits>::operator >>(std::basic_streambuf<_elem,_traits> *)' 1> 1> [ 1> _elem=char, 1> _traits=std::char_traits<char> 1> ] 1> while trying match argument list '(std::istream, const int)' 1>c:\cpp\stream_test\stream_test\main.cpp(7) : fatal error c1903: unable recover previous error(s); stopping compilation 1>build log saved @ "file://c:\cpp\stream_test\stream_test\debug\buildlog.htm" 1>stream_test - 2 error(s), 0 warning(s) ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
because can't read const variable. overloads of operator>>
take lvalue references non-const variables second parameters , const int
can't bind int&
. additionaly, operator>>
returns reference left hand operand, , can't use initilize integer.
you can write function:
int get_from(std::istream& is) { int i; if (is >> i) { return i; } else { // error, might wanna throw here } }
and use
a(std::istream& strm) : n(get_from(strm)) {}
but i'd rethink whether want const
member variable. it'll make class non-coypable.
Comments
Post a Comment