C++ifstream来构建对象
C++ ifstream to build objects
对于一项作业,我被要求从两个独立的.dat文件中读入。我一直在使用ifstreams来实现这一点,但在尝试构建时,我收到以下错误消息:/usr/include/c++/4.8/bits/ios_base.h:786:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
。我已经设法追踪到了这条违规线路:NMEAobject fred = get_nmea_object(in_stream1);
这是我的头文件
#ifndef NMEAINPUT_H
#define NMEAINPUT_H
class NMEAinput {
public:
NMEAinput(std::string fn1, std::string fn2);
private:
std::ifstream in_stream1;
std::ifstream in_stream2;
std::ofstream out_stream;
bool test_gsv(std::string in_gsv);
NMEAobject get_nmea_object(std::ifstream in);
NMEAobject read_rmc(std::string in_rmc);
std::string* split(std::string in_string,char split_point);
};
#endif
这是我的cpp文件:
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <list>
#include "NMEAobject.h"
#include "NMEAinput.h"
#include "GPXoutput.h"
NMEAinput::NMEAinput(std::string fn1, std::string fn2) {
in_stream1.open(fn1.c_str());
in_stream2.open(fn2.c_str());
out_stream.open("outfile.gpx");
if(!in_stream1||!in_stream2) {
std::cout << "Error with data read";
}
std::list<NMEAobject> stream1;
std::list<NMEAobject> stream2;
NMEAobject fred = get_nmea_object(in_stream1);
stream1.push_back(fred);
stream2.push_back(get_nmea_object(in_stream2));
int stream_time = stream1.front().get_time();
stream1.pop_front();
stream1.push_back(get_nmea_object(in_stream1));
stream2.push_back(get_nmea_object(in_stream2));
GPXoutput out(out_stream);
while(!stream1.empty() && !stream2.empty()){
if(stream1.front().get_time() - stream_time < 1){
out.put(stream1.front().convert(),out_stream);
}
else{
out.put(stream2.front().convert(),out_stream);
}
stream_time = stream1.front().get_time();
stream1.pop_front();
stream2.pop_front();
stream1.push_back(get_nmea_object(in_stream1));
stream2.push_back(get_nmea_object(in_stream2));*/
// if(stream1.front() == std::NULL || stream2.front() == std::NULL) break;
}
}
NMEAobject NMEAinput::get_nmea_object(std::ifstream in_stream){
std::string line;
bool good_rmc = false;
bool good_gsv = true;
while(std::getline(in_stream,line)&&!good_rmc){
std::string data_check = line.substr(0,3);
if(data_check == "$GP"){
data_check = line.substr(3,3);
if(data_check == "GSV"){
good_gsv = test_gsv(line);
}
else if (data_check =="RMC"&&good_gsv){
NMEAobject out = read_rmc(line);
good_rmc = true;
return out;
}
}
}
}
iostream不可复制,但您通过值传递它们:
NMEAobject get_nmea_object(std::ifstream in); // Wrong
应替换为:
NMEAobject get_nmea_object(std::ifstream& in);
以上内容现在通过引用,因此不会产生副本。
基本上,你的问题可以归结为这样做(试着编译这个小程序(。你会看到基本上与现在相同的错误。
#include <fstream>
using namespace std;
int main()
{
ifstream ifs1;
ifstream ifs2;
ifs1 = ifs2;
}
相关文章:
- 如何为 mlpack 的 kmeans 构建 HamerlyKmeans 对象
- 如何在构建对象堆栈时解决转换错误?
- 生成文件不构建对象,我告诉它如何
- 使用C对象指针构建PyObject*
- 从单个.cpp构建共享对象
- 仅在函数内部构建对象时EXC_BAD_ACCESS
- lambda 对象构建成本高吗?
- 如何构建程序以避免查询对象类型?
- 尝试与从 CUDA 对象构建的共享库链接时未定义的符号
- 是否有任何可能的方法将事件(自定义)附加到在运行时创建的对象?[C 构建器]
- C 如何停止对象构建失败的执行
- 通过值和参考,对象构建返回对象之间的区别
- 如何将项目共享"system calls"作为单例对象构建和链接到引导加载程序?
- 使用操作员= C 对象构建
- cmake,我可以从可执行对象构建一个库吗?
- 为什么由指针和对象构建的局部变量显示不同的输出
- 如何为我在C++中创建的对象构建迭代器
- 在OpenCV中从Mat对象构建图像
- 延迟C++静态对象构建-Linux上的GCC
- 当从固定结构体中存储的对象构建函数时,c++ lambda构建静态函数返回意外值