2016-12-24 20 views
0

大きなコードでバグを追跡するのに時間を費やしました。私はそれを小さなファイルに圧縮しました。クリーンなコードのメンバ変数としてfstreamを使用する必要があります。オンラインリソースは、これがうまくいくと言います。私も成功して.open()でfstreamを初期化しようとしました。私はg ++でubuntu 16.04をコンパイルしています。 C++のバージョンでfstreamクラスメンバ変数

#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第一は、コピーコンストラクタを使用してrにコピーし、その後、class readの無名のインスタンスを作成する前に。 C++の標準ストリームはコピー可能ではないため、読み込みはコピーできません。したがって、コピーコンストラクタを使用しようとするとエラーが発生します。

C++ 11以上のバージョンでは、標準のストリームが移動可能だがコピーできないため、このコードを有効にする移動コンストラクタを使用します。 read r(fileName);を使用すると、どちらのコンストラクタもすべてのバージョンで使用できなくなり、その代わりにrを構築します。

+0

はsmith_61とlatedeveloperありがとうございます。私はcでもっと家族になり、cppで家族になるように努力しています。私は自分のコンストラクタをその形式で使ったことはありません。 –

+0

大きなコードを2秒で無駄にしました。再度、感謝します –

1

ストリームオブジェクトがコピー可能ではありませんので、あなたが言うことができません:ストリームオブジェクトが含まれているreadオブジェクトの

read r = read(fileName); 

。また、この:

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

は次のようになります。

read(string fileName): infile(fileName.c_str()) {} 
関連する問題