问题
I have tried to write a code to read strings from file named "test.txt" and write the strings to standard output. The code below works well:
int main()
{
using namespace std;
ifstream file("test.txt");
copy(istream_iterator<string>(file),
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
However, with this modification, the code no longer compiles:
int main()
{
using namespace std;
copy(istream_iterator<string>(ifstream("test.txt")), // <-- Error here
istream_iterator<string>(),
ostream_iterator<string>(cout, " "));
}
Why doesn't this version compile?
The compiler I used is g++4.6.2, and the error as below:
ex11-16.cpp:16:65: error: no matching function for call to 'std::istream_iterator<std::basic_string<char> >::istream_iterator(std::ifstream)'
ex11-16.cpp:16:65: note: candidates are:
.../bits/stream_iterator.h:72:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist> = std::istream_iterator<std::basic_string<char> >]
.../bits/stream_iterator.h:72:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'const std::istream_iterator<std::basic_string<char> >&'
.../bits/stream_iterator.h:68:7: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type&) [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int, std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_type = std::basic_istream<char>]
.../bits/stream_iterator.h:68:7: note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'std::istream_iterator<std::basic_string<char> >::istream_type& {aka std::basic_istream<char>&}'
.../bits/stream_iterator.h:64:26: note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator() [with _Tp = std::basic_string<char>, _CharT = char, _Traits = std::char_traits<char>, _Dist = int]
.../bits/stream_iterator.h:64:26: note: candidate expects 0 arguments, 1 provided
回答1:
There are (I believe) two errors here:
(1) You need to put quotations around test.txt
:
istream_iterator<string>(ifstream("test.txt"), ... );
(2) istream_iterator
's constructor takes in an istream&
(that is, an lvalue reference to a stream). Consequently, the stream that you pass in has to be an lvalue. However, passing in ifstream("test.txt")
passes in a temporary object of type ifstream
, which is an rvalue rather than an lvalue. This is the same reason that you can't do this:
int function(int& x) {
x++;
}
int main() {
function(137); // Error - 137 is an rvalue, but lvalue is needed.
}
Hope this helps!
来源:https://stackoverflow.com/questions/17246720/why-does-istream-iteratorstringifstreamtest-txt-cause-an-error