Wednesday 15 August 2012

c++ - Why does istream_iterator(ifstream("test.txt")) cause an error? -


I have tried to write a code to read the string from a file named "test.txt" and in the standard output Have to write The code below works well:

  int main () {namespace std; Ifstream file ("test.txt"); Copy (istream_iterator & lt; string & gt; (file), istream_iterator & lt; string & gt; (), ostream_iterator & lt; string & gt; (cout, "")); }   

However, with this modification, the code is no longer compiled:

  int main () {namespace std; Copy (istream_iterator & lt; string & gt; (ifstream ("test.txt")), // & lt; here error istream_iterator & lt; string & gt; (), ostream_iterator & lt; string & gt; (court , "")); }   

Why does not this version compile?

The compiler I used is G ++ 4.6.2, and error as below:

  ex11-16.cpp: 16: 65: Error: Calling For 'std :: istream_iterator & lt; Std :: basic_string & lt; Char & gt; & Gt; :: istream_iterator (std :: ifstream) ex11-16.cpp: 16: 65: Note: Candidates are: ... / bits / stream_iterator.h: 72: 7: note: std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; :: istream_iterator (constant std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; & amp;) [with _Tp = std :: basic_string & lt; Four>, _CharT = four, _Traits = std :: char_traits & lt; Four>, _Dist = int, std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; = Std :: istream_iterator & lt; Std :: origin_string & lt; Char & gt; 'Gtc: std :: basic_string & lt; Four & gt; & Gt; & Amp; '... / bits / stream_iterator.h: 68: 7: note: std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; :: istream_iterator (std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; :: istream_type & amp;) [with _Tp = std :: basic_string & lt; Four>, _CharT = four, _Traits = std :: char_traits & lt; Four>, _Dist = integer, std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; :: istream_type = std:: original_stream & lt; Char & gt;] ... / bits / stream_iterator.h: 68: 7: Note: 'std :: ifstream {aka std :: basic_ifstream & lt; Char & gt;} there is no known conversion for 'logic' std :: istream_iterator & lt; Std :: basic_string & lt; Four & gt; & Gt; :: istream_type & amp; {Aka std :: basic_istream & lt; Four & gt; & Amp;} '... / bits / stream_iterator.h: 64: 26: note: std :: istream_iterator & lt; _Tp, _CharT, _Traits, _Dist & gt; :: istream_iterator () [_Tp = std :: basic_string & lt; Four>, _CharT = four, _Traits = std :: char_traits & lt; Four>, _Dist = integer] ... / bits / stream_iterator.h: 64: 26: note: candidate 0 logic, expected to provide    

< There are two errors (I believe):

(1) You have test.txt :

  istream_iterator & lt; String & gt; (Ifstream ("test.txt"), must be quoted around ...;   

(2) istream_iterator creator of a istream & amp; ; in (one reference reference for a stream), as a result, the stream in which you pass should be a new one, however, ifstream ("test.txt") is passing in. passes through a temporary object type ifstream , which is rvalue lvalue . This is the reason you can not do this: < / P>

  int function (int & x) {x ++;} int main () {function (137); // Ruti - 137 is a Ravlu, but requires Larvel}   

Hope it helps!

No comments:

Post a Comment