c++11 - 错误 : use of deleted function ‘std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const std::basic_ofstream<char>&)’ ofstream deleted

标签 c++11 g++

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 编译它,这会导致下面相同的错误消息。

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>

最佳答案

移动 iostream 的支持已添加到 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 |流缓冲区 |是 |
27.7 |格式化和操纵器 |部分|缺少移动和交换操作缺少 get_time 和 put_time 操纵器。
27.8 |基于字符串的流 |部分|缺少移动和交换操作
27.9 |基于文件的流 |部分|缺少移动和交换操作

它在 GCC 5.x 中受支持并且工作正常,所以你一定是做错了什么(可能忘记使用 -std=c++11,或者将它指向 4.9 header 肯定不行)。

关于c++11 - 错误 : use of deleted function ‘std::basic_ofstream<char>& std::basic_ofstream<char>::operator=(const std::basic_ofstream<char>&)’ ofstream deleted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088414/

相关文章:

c++ - 具有不同签名的绑定(bind)函数

C++ 11 动态数组部分列表初始化(错误或误解)?

c++ - 在 C++ 中包含 C 代码

c++ - 从 unique_ptr 创建 shared_ptr

c++ - 为什么 g++ 4.6.2 仍然需要 "-std=c++0x"?

c++ - 将 C++ 代码从 MS Visual Studio 移植到 Linux 并处理类型化枚举

c++ - 使用智能指针变得聪明 : avoiding shared_ptr overuse

c++ - 为什么调用复制构造函数而不是 move 构造函数?

c++ - C++ 中的结构偏移量和指针安全

c++ - SIGSEGV 带有指向模板对象的指针的 std::map