2017-04-15 12 views
0

std::ofstreamのラッパーであるクラスfileを作成しました。 Containerを作成して、fileのすべてのインスタンスを格納しました。ofstreamのインスタンスをコンテナに挿入

class file 
{ 
private: 
     std::ofstream ofs; 

public: 
     void open(std::string filename); 
     void write(std::string s); 
     void close(); 
}; 

class Container 
{ 
private: 
    std::map<int, file> m; 


public: 
     void insert(file f,int i) 
     { 
      m.insert(std::pair<int,file> (i,f)); 
     } 
     void get(int i) 
     { 
      m.at(i); 
     } 
}; 

ただし、このコードには問題があります。 insertメソッドでは、std::pair<int,file>をコピーしようとしています。これは、std::ofstreamのコピーコンストラクタが削除されているため、実行できません(下記のコンパイルエラー参照)。

fileのインスタンスをコンテナに連続して追加したいと思います。どうやってやるの?ここで


あなたが一種のそれらをコピーせずにfileであなたのContainerを初期化したい場合は、あなたがstd::ofstreamの移動、コンストラクタに依存している可能性があり、コンパイルエラー

In file included from src/test.cpp:1: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439: 
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:627: 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:274:23: error: call to implicitly-deleted copy constructor of 
     'file' 
     : first(__x), second(__y) {} 
        ^ ~~~ 
src/test.cpp:35:22: note: in instantiation of member function 'std::__1::pair<int, file>::pair' requested here 
      m.insert(std::pair<int,file> (i,f)); 
        ^
src/test.cpp:9:21: note: copy constructor of 'file' is implicitly deleted because field 'ofs' has a deleted copy constructor 
     std::ofstream ofs; 
        ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/fstream:1168:5: note: copy constructor is implicitly deleted because 
     'basic_ofstream<char, std::__1::char_traits<char> >' has a user-declared move constructor 
    basic_ofstream(basic_ofstream&& __rhs); 
    ^
1 error generated. 
+0

メインコードで 'Container :: insert'メソッドをどう呼びますか? – Holt

+0

私はそれが重要であるとは思わなかった。 'Container'はシングルトンです。私は' MyContainer.insert(file、index) 'を使います。 –

+0

そして、あなたが挿入する 'ファイル'をどうやって作りますか?あなたはそれをコピーしたいのですか、それとも 'Container'に入れるだけですか?コンテナ内に 'file'を作成して挿入する典型的な例を教えてください。 – Holt

答えて

1

です:

void insert(file &&f, int i) 
{ 
    m.insert(std::pair<int,file>(i, std::move(f))); 
} 

次に、以下を実行します。

cont.insert(file{}, 0); // Construct a file when inserting, you already have an rvalue 

または:

file myfile; 
myfile.open(...); // Do whatever you want with myfile... 
cont.insert(std::move(myfile), 0); // And then move it, as long as you are not using if after 

あなたfileは移動-構成可能ではなく、あなたの場所でそれを構築することができている場合:

template <typename... Args> 
void insert(int i, Args&&... args) { 
    m.emplace(std::piecewise_construct, std::tuple<int>(i), 
       std::tuple<Args>(std::forward<Args>(args)...)); 
} 

また、挿入したい場合(最初の例のように)デフォルトで初期化されたファイルは、単に次のようにすることができます:

void insert(int i) 
{ 
    m[i]; 
} 
関連する問題