2017-03-29 21 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头文件肯定无法工作)。

相关问题