c++ - 为什么ofstream作为类成员不能传递给线程?
问题描述
我写了一个带有 operator() 重载的类,我想像函数指针一样将这个类传递给线程,所以我把它放在线程中,如下所示。但是,它无法编译,我注意到 ofstream 是它失败的原因。为什么这是错误的?
#include <thread>
#include <fstream>
using namespace std;
class dummy{
public :
dummy(){}
void operator()(){}
private:
ofstream file;
};
int main()
{
dummy dum;
thread t1(dum);
return 0;
}
解决方案
因为std::basic_ofstream
复制构造函数被删除了,看这里。因此,您的dummy
类复制构造函数也被隐式删除。您需要移动对象而不是复制它:
std::thread t1(std::move(dum));
推荐阅读
- postgresql - PostgresQL 错误:关系不存在
我是 Postgres 和 plpsql 的新手,如果答案很明显,我很抱歉。但是我无法弄清楚我在这里做错了什么。当我使用示例 DB 函数时,一切都很好。当我尝试创建自己的:
CREATE OR REPLACE FUNCTION get_trans (p_param VARCHAR) RETURNS TABLE ( amount money, transactionId UUID
- php - WordPress OPTIONS 保存了错误的输入
- java - Add text before every vowel
- c# - 如何对文件或文档进行单元测试?
- mysql - 从规范化表中获得近乎实时的更新
- macos - Mac shell脚本删除文件
- pandas - 如何旋转熊猫数据框?
- css - Wordpress:移动设备上的文本消失
- image - 从地图设计网站获取几何信息和名称
- android - 为什么懒惰的kotlin会导致android中的内存泄漏?