2017-04-15 123 views
0

我創建了我的類file,它是std::ofstream的包裝。我創建了一個Container來包含所有file的實例。將流的實例插入容器

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的實例。我怎樣才能做到這一點?


以下是編譯錯誤

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(文件,索引)'。 –

+0

你如何創建你插入的'file'?你想複製一份還是放入'Container'?你能告訴我們一個典型的'文件'創建和插入容器內的例子嗎? – Holt

回答

1

如果你想那種初始化Containerfile無需將其複製,那麼你可以依靠的std::ofstream移動構造函數:

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]; 
}