operator=不适用于fstream
operator = does not work with fstream
我有一个结构
struct myStruct {
fstream fp;
char *buffer;
size_t size;
};
我是C++的新手,正在尝试编写一个代码,其中一个线程将从一个文件读取到缓冲区,主线程将缓冲区写入另一个文件。代码示例如下:
int main() {
pthread tid1;
struct myStruct readArgs;
fstream fileP, fileP2;
fileP.open("/tmp/20MBFile", fstream::in);
fileP2.open("/tmp/trial-file", fstream::out);
char *buffer;
readArgs.fp = fileP;
readArgs.buffer = buffer;
readArgs.size = 1048576;
pthread_create(&tid1, NULL, Read, &readArgs);
pthread_join(tid1, NULL);
fileP2.write(buffer, 1048576);
......
}
读取功能如下:
void *Read(struct myStruct *readArgs) {
readArgs->fp.read(readArgs->buffer, readArgs->size);
pthread_exit(NULL);
}
然而,当我编译代码时,我会出现以下错误:
错误:使用已删除的函数"std::basic_stream&std::basic_stream::operator=(const std::basic_stream&)'readArgs.fp=文件P;
和
错误:从"void*()(myStruct)"到"void*pthread_create(&tid1,NULL,Read,&readArgs);^在/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr default.h:35:0中包含的文件中,从/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../include/c++/4.8.3/x86_64-redhat-linux/bits/gthr.h:148,从/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../include/c++/4.8.3/ext/atomicity.h:35,从/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../include/c++/4.8.3/bits/ios_base.h:39,从/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../include/c++/4.8.3/ios:42,从/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../include/c++/4.8.3/ostream:38,来自/usr/lib/gcc/x86_64-redhat-
……
/usr/include/phread.h:232:12:错误:初始化"int pthread_create(pthread_t*,const pthread_attr_t*,void*()(void),void*)"的参数3[-fpermission]extern int pthread_create(pthread_t*__restrict__newthread,
我是不是遗漏了什么?提前感谢!
找到了答案
1.不能使用=分配fstream
-fstream
,但可以移动。由于gcc
版本的原因,swap()
也不起作用。我有gcc版本4.8.3.x,我想它不支持fstream
的swap()
调用
2.pthread_create()
-它需要签名为void *foo(void *)
的函数,因此必须传递所需的参数,并在所需函数中进行类型转换
在这里我会这样做:
pthread_create(&tid1, NULL, Read, &readArgs);
Read()
的功能是:
void *Read(void *args) {
myStruct *readA = (myStruct *)args;
....
}
感谢大家抽出时间!
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 适用于win32、linux、mac的POSIX C包装器
- WinDBG适用于从Visual Studio 2015保存的转储,但不适用于任务管理器。显示异常代码"not found"
- 从uint8_t到NPY_UINT16 PyArray_SimpleNewFromData.适用于Linux,但不适用于