【问题标题】:How to get rid of no "matching function call error" when iterating over a stream buffer?迭代流缓冲区时如何摆脱“匹配函数调用错误”?
【发布时间】:2019-06-03 12:17:42
【问题描述】:

我正在尝试通过迭代流缓冲区的每个元素,将应该具有 std::complex 类型的二进制数据存储到向量中。但是我一直收到错误提示

no matching function for call to ‘std::istreambuf_iterator<std::complex<float> >::istreambuf_iterator(std::ifstream&)’
 std::for_each(std::istreambuf_iterator<std::complex<float> >(i_f1),

我已尝试寻找解决方案,但找不到任何可行的方法。我也在尝试遵循How to read entire stream into a std::vector? 中给出的示例。此外,我正在使用 g++ 和 -std=c++11 进行编译。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <boost/tuple/tuple.hpp>
#include <algorithm>
#include <iterator>

int main(){

    //path to files
    std::string data_path= "/$HOME/some_path/";

    //file to be opened
    std::string f_name1 = "ch1_d2.dat";


    std::ifstream i_f1(data_path + f_name1, std::ios::binary);
    if (!i_f1){
        std::cout << "Error occurred reading file "<<f_name1 <<std::endl;        std::cout << "Exiting" << std::endl;
        return 0;
     } 

    //Place buffer contents into vector 
    std::vector<std::complex<float> > data1;

    std::for_each(std::istreambuf_iterator<std::complex<float> >(i_f1), 
        std::istreambuf_iterator<std::complex<float> >(), 
        [&data1](std::complex<float> vd){
        data1.push_back(vd);
        });

   // Test to see if vector was read in correctly
   for (auto i = data1.begin(); i != data1.end(); i++){
        std::cout << *i << " ";

    }

    i_f1.close();
    return 0;
}

我对自己做错了什么很迷茫,因此想知道为什么

std::istreambuf_iterator() 

不接受我提供的流作为参数? 此外,错误消息让我感到困惑,因为它似乎暗示我以错误的方式调用该函数,或者一个不存在的函数。

谢谢

【问题讨论】:

    标签: c++ iteration


    【解决方案1】:

    您想使用operator&gt;&gt; 读取i_f1 中的std::complex 从i_f1(这是一个std::ifstream),所以您需要一个std::istream_iterator 而不是std::istreambuf_iterator1:

    std::for_each(std::istream_iterator<std::complex<float> >(i_f1), 
        std::istream_iterator<std::complex<float> >(), 
        [&data1](std::complex<float> vd){
            data1.push_back(vd);
        });
    

    您的代码实际上可以简化为:

    std::vector<std::complex<float>> data1{
        std::istream_iterator<std::complex<float>>(i_f1), 
        std::istream_iterator<std::complex<float>>()};
    

    1std::istreambuf_iterator 用于在每个字符上迭代每个字符,例如 std::basic_istream,而不是使用 operator&gt;&gt; 的重载来迭代它。

    【讨论】:

    • 实际上,istreambuf_iterator 可以有意地从 std::istream& 实例化...但它必须具有相同的字符类型。
    • @LightnessRacesinOrbit 谢谢,我实际上从未使用过streambuf_iterator,所以我不知道它的真正用途。我已经更新了答案。
    【解决方案2】:

    您可能使用了错误的工具来完成这项工作。

    您正在尝试使用缓冲区迭代器,它迭代流的缓冲区 的组成部分。但是您告诉您的计算机缓冲区是complex&lt;float&gt;s 之一……它不是。 ifstream 的缓冲区属于 chars。因此,您尝试使用的构造函数(采用ifstream 和complex&lt;float&gt; 缓冲区的构造函数)不存在。

    您可以使用istream_iterator 执行格式化 迭代,即使用流的魔力(在这种情况下,将输入词法解释为complex&lt;float&gt;s)而不是直接访问其底层字节。

    您可以阅读上一个问题"the difference betwen istreambuf_iterator and istream_iterator"的更多信息。

    您链接到的示例也可以解释这一点。

    【讨论】:

      猜你喜欢
      • 2018-08-01
      • 2012-01-03
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多