fstream类成员变量 [英] fstream class member variable

查看:95
本文介绍了fstream类成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了数小时来查找较大代码中的错误.我已将其压缩为一个小文件.我需要使用fstream作为干净代码的成员变量.在线资源说这应该可行.我也尝试过使用.open()初始化fstream,但没有成功.我正在使用g ++在ubuntu 16.04上进行编译.

I spent hours tracking down a bug in a larger piece of code. I have compressed it into a small file. I need to use an fstream as a memeber variable for clean code. Resources online say this should work. I have also tried initializing fstream with .open() with no success. I am compiling on ubuntu 16.04 with g++.

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

class read{
    private:            
        ifstream infile;
    public:
        read(string fileName): infile(fileName.c_str());}
        ~read(){infile.close();}
};

int main(){
    string fileName = "./test/FileCreator/SourceTEST.cpp";
    read r = read(fileName);

return 0;
}

编译器错误

/usr/include/c++/5/bits/ios_base.h: In copy constructor ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’:
/usr/include/c++/5/bits/ios_base.h:855:5: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
     ios_base(const ios_base&);
     ^
In file included from /usr/include/c++/5/ios:44:0,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/fstream:38,
                 from smallTestRead.cpp:2:
/usr/include/c++/5/bits/basic_ios.h:67:11: error: within this context
     class basic_ios : public ios_base
           ^
In file included from smallTestRead.cpp:2:0:
/usr/include/c++/5/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/5/fstream:455:11: note: synthesized method ‘std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)’ first required here 
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
In file included from /usr/include/c++/5/ios:43:0,
                 from /usr/include/c++/5/istream:38,
                 from /usr/include/c++/5/fstream:38,
                 from smallTestRead.cpp:2:
/usr/include/c++/5/streambuf: In copy constructor ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’:
/usr/include/c++/5/streambuf:804:7: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]’ is private
       basic_streambuf(const basic_streambuf&);
       ^
In file included from smallTestRead.cpp:2:0:
/usr/include/c++/5/fstream:72:11: error: within this context
     class basic_filebuf : public basic_streambuf<_CharT, _Traits>
           ^
/usr/include/c++/5/fstream: In copy constructor ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’:
/usr/include/c++/5/fstream:455:11: note: synthesized method ‘std::basic_filebuf<char>::basic_filebuf(const std::basic_filebuf<char>&)’ first required here 
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
smallTestRead.cpp: In copy constructor ‘read::read(const read&)’:
smallTestRead.cpp:7:7: note: synthesized method ‘std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)’ first required here 
 class read{
       ^
smallTestRead.cpp: In function ‘int main()’:
smallTestRead.cpp:17:24: note: synthesized method ‘read::read(const read&)’ first required here 
  read r = read(fileName);

推荐答案

流对象不可复制,因此您不能说:

Stream objects are not copyable, so you cannot say:

  read r = read(fileName);

表示包含流对象的 read 对象.另外,这个:

for a read object that contains a stream object. Also, this:

read(string fileName): infile(fileName.c_str());}

应为:

read(string fileName): infile(fileName.c_str()) {}

这篇关于fstream类成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆