2017-03-29 23 views
1
std::map<std::string, std::ofstream> Map; 
std::string name="name"; 
std::ofstream ofs(name,std::ios::app); 
Map[name] = std::move(ofs); 

我運行上面的代碼,但失敗了。 我使用-std = C++ 11在Ubuntu12.04和g ++-5(gcc版本5.4.1 20160904(Ubuntu 5.4.1-2ubuntu1〜12.04))上通過g ++ 4.9進行編譯,它在下面引入了相同的錯誤消息。誤差:使用刪除功能的「標準:: basic_ofstream <char>&的std :: basic_ofstream <char> ::運算符=(常量的std :: basic_ofstream <char>&)」的ofstream刪除

error: use of deleted function ‘std::basic_ofstream& std::basic_ofstream::operator=(const std::basic_ofstream&)’ Map[name] = std::move(ofs);

/usr/include/c++/4.9/fstream:602:11: note: ‘std::basic_ofstream& std::basic_ofstream::operator=(const std::basic_ofstream&)’ is implicitly deleted because the default definition would be ill-formed: class basic_ofstream : public basic_ostream<_CharT,_Traits>

+0

看起來是g ++版本特定的。用VS 2013和[g ++ - 5.1]構建(http://coliru.stacked-crooked.com/a/c83e8a3939ac3115)。 – acraig5075

回答

3

支持移動iostreams被添加到GCC 5.1,所以你不能用GCC 4.9來做到這一點。這記錄在版本4.9的libstdC++手冊中:https://gcc.gnu.org/onlinedocs/gcc-4.9.4/libstdc++/manual/manual/status.html#status.iso.2011

27.5 | Iostreams基類|部分|在basic_ios上缺少移動和交換操作。缺少io_errc和iostream_category。 ios_base :: failure不是從system_error派生的。缺少ios_base :: hexfloat。
27.6 |流緩衝區| Y |
27.7 |格式化和操作符|部分|缺少移動和交換操作缺少get_time和put_time操縱器。
27.8 |基於字符串的流|部分|缺少移動和交換操作
27.9 |基於文件的流|部分|缺少移動和交換操作

它在GCC 5.x中支持並且工作正常,所以你必須做錯了某些事情(可能忘記使用-std=c++11或指向它的4.9頭文件肯定無法工作)。

相關問題