2016-12-24 108 views
0

我花了几个小时来跟踪更大代码中的错误。我把它压缩成一个小文件。我需要使用fstream作为清洁代码的memeber变量。网上资源说这应该工作。我也尝试用.open()初始化fstream而没有成功。我正在使用g ++编译Ubuntu 16.04。前Cfstream类成员变量

#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); 

回答

1

read r = read(fileName);上的C++版本++ 11首先创建的class read一位不愿透露姓名的实例,然后使用拷贝构造函数其拷贝到r。 C++的标准io流不可复制,这使得读取不可复制。因此,您尝试使用复制构造函数的错误。

高于C++ 11的版本将使用移动构造函数,这将使此代码有效,因为标准io流是可移动的但不可复制。使用read r(fileName);将阻止任何构造函数在所有版本中使用,而是构造r

+0

谢谢smith_61和latedeveloper。我更喜欢c,并试图用cpp成为家庭。我从来没有用过这种格式的自己的构造函数。 –

+0

我在2秒钟内修复了我的较大代码浪费。再次感谢 –

1

流对象是不可拷贝,所以你不能说:

read r = read(fileName); 

为包含流对象read对象。此外,这样的:

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

应该是:

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