【发布时间】: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()
不接受我提供的流作为参数? 此外,错误消息让我感到困惑,因为它似乎暗示我以错误的方式调用该函数,或者一个不存在的函数。
谢谢
【问题讨论】: