我正在 Visual Studio Code 中使用 C++ 中的叉子和管道编写一个项目,当我编写代码时
int temptotal;
write(fds[1],temptotal,sizeof(int));
或
read(fds[0],temptotal,sizeof(int));
我在 temptotal 变量上看到红色下划线,表示: '“int”类型的参数与“const void *”类型的参数不兼容'
如有必要,我可以提供有关该项目的更多详细信息,但我相信只有这个单独的方面存在问题,因为其他一切都按预期进行,但我无法在孩子和父母之间进行 read() 和 write() 。我认为 const void * 允许我传递任何类型,但我显然误解了一些东西。我正在使用 Visual Studio Code 使用 g++ 编译器。
您需要提供一个指向内存的指针,其中
write
应写入且read
应读取:
write(fds[1], &temptotal, sizeof temptotal);
// ^
read(fds[0], &temptotal, sizeof temptotal);
// ^