c++ - 错误错误: ‘void*’ is not a pointer-to-object type

标签 c++ linux operating-system pthreads

大家好,我在代码中遇到一个小问题

这个程序应该计算 ( (a+b) x (c+d) )/e

创建三个线程,其中一个用于 仅限加法 仅乘法 部门

main() 中只能创建一个线程 在除法线程中显示结果。 int 值(a、b、c、d、e)应从 main 中的用户处获取,并传递到 main 中创建的线程上。每个步骤的其他结果应传递到下一步。

这是我为上述场景编写的程序

#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void *Division(void *arg_div)
{
    int result = arg_div[0]/arg_div[1];
    cout << result ;
    pthread_exit(NULL);
}

void *Multiplication(void *arg_mul)
{
    int arg1[2];
    arg1[0]=arg_mul[0]*arg_mul[1];
    arg1[1]=arg_mul[2];
    pthread_t div;
    pthread_create(&div,NULL,Division,(void*)arg1);
    pthread_join(div,NULL);
    pthread_exit(NULL);
}

void *Addition(void *arg_add)
{
    int arg[3];
    arg[0]=arg_add[0]+arg_add[1];
    arg[1]=arg_add[2]+arg_add[3];
    arg[2]=arg_add[4];
    pthread_t ad;
    pthread_create(&ad,NULL,Multiplication,(void*)arg);
    pthread_join(ad,NULL);
    pthread_exit(NULL);
}

int main()
{
    int values[5];
    for(int i=0;i<5;i++)
    {
        cin >> values[i];
    }
    pthread_t pa;
    pthread_create(&pa,NULL,Addition,(void*)values);
    pthread_join(pa,NULL);
    return 0;
}

最佳答案

为什么将指针类型(C 风格...)更改为 void*? (刁钻的问题让你走在正确的道路上)。

只需取回 int* 即可,它们是可以取消引用的指针。 void* 是指针,但你无法获取 void 对象,更不用说对其进行操作了。

int* args = static_cast<int*>(arg);

关于c++ - 错误错误: ‘void*’ is not a pointer-to-object type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53589948/

相关文章:

c++ - 在 OpenGL 中删除部分绘图

c++ - 使用多列索引优化选择查询

c++ - 如何使用 Qt 类编译 rpcgen 文件?

linux - Windows 内存页面是否始终以 4096 字节为单位?

c++ - 关于 Javascript 引擎错误的问题

python - 从/sdb1读取数据

linux - 如何从匹配行之后删除文件中的所有行?

java - 当你运行java字节码时操作系统会做什么?它如何与 JVM 交互?

operating-system - 处理器如何处理除以零的情况

memory - 为什么我无法分配 RAM 位数?