2016-12-24 88 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()) {}