本文介绍了为什么istream_iterator< string>(ifstream("test.txt")))会导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写代码以从名为"test.txt"的文件中读取字符串,并将字符串写入标准输出.下面的代码运行良好:

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?

我使用的编译器是g ++ 4.6.2,错误如下:

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

推荐答案

(我认为)这里有两个错误:

There are (I believe) two errors here:

(1)您需要在test.txt周围加上引号:

(1) You need to put quotations around test.txt:

istream_iterator<string>(ifstream("test.txt"), ... );

(2)istream_iterator的构造函数采用istream&(即对流的左值引用).因此,您传入的流必须是左值.但是,传入ifstream("test.txt")会传入类型为ifstream的临时对象,该对象是 rvalue 而不是 lvalue .这是您无法执行此操作的相同原因:

(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.
}

希望这会有所帮助!

这篇关于为什么istream_iterator&lt; string&gt;(ifstream("test.txt")))会导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:19