c++ - 错误: invalid conversion from 'int' to 'void*' [-fpermissive]

标签 c++

为我必须使用线程的实验室构建一个程序,我有点迷失了,但我即将编译它。我有两个错误:一个在标题中提到,另一个是同样的事情,但它说从 'void*' 到 'int' 的转换无效。

错误发生在生产者线程和消费者线程中的第 98 行和第 124 行,我已在代码中标记了它们。这是相当多的代码,但我不知道如何真正缩小它,抱歉。

// syncA.cpp for lab2

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

#define BSIZE 10
#define NUM_ITEMS 10
#define NUM_THREADS 2


int buf[BSIZE];
int nextin=0, nextout=0;


void * producer(void *);    // function for producer thread
void * consumer(void *);    // function for consumer thread
pthread_mutex_t lock;

pthread_t tid[NUM_THREADS];      // array of thread IDs


int main( int argc, char *argv[] ) 
{
    int i;

    cout << "Creating threads" << endl;

    pthread_create(&tid[1], NULL, consumer, (void *) buf[BSIZE]);
    pthread_create(&tid[0], NULL, producer, (void *) buf[BSIZE]);

    for (i = 0; i < NUM_THREADS; i++){
        pthread_join(tid[i], NULL);
    }

    cout << "All threads have been terminated" << endl << endl;

    // Finding minimum
    int minimum = buf[1];

    for (i = 1; i <= BSIZE; i ++){
        if (minimum > buf[i + 1])
            minimum = buf[i + 1];
    }

    // Finding maximum
    int maximum = buf[1];

    for (i = 1; i <= BSIZE; i++){
        if (maximum < buf[i + 1])
            maximum = buf[i + 1];
    }

    // Finding average
    int average;
    int sum = 0;

    for (i = 1; i <= BSIZE; i++){
        sum = sum + buf[i];
    }

    average = sum / BSIZE;

    // Outputting claculated data
    cout << "Minimum value: " << minimum << endl;
    cout << "Maximum value: " << maximum << endl;
    cout << "Average value: " << average << endl;

    return 0;

}  /* main */



void * producer(void * buf[])
{
    int product; // For multiplying inside the for loop for the "wait"
    int num;

    cout << "Producer started" << endl;

    // Locking thread
    pthread_mutex_lock(&lock);

    // Producing 10 items and putting them in the buffer
    for (int i = 0; i < BSIZE; i++){
        num = rand() % 1000;

        // Using a for loop 1000 times to act as the wait
        for (int k = 0; k < 1000; k++){
            product = 8 * 9;
        }

        // Putting the num in the buffer at pos 1, 2, 3, etc
        buf[nextin++] = num; <---------------- ***ERROR***
    }
    // Unlocking thread
    pthread_mutex_unlock(&lock);

    // Exiting the producer
    pthread_exit(0);
}    

void * consumer(void * buf[])
{
    int num;
    int product;

    cout << "Consumer started" << endl << endl;

    // Locking thread
    pthread_mutex_lock(&lock);

    // Waiting before accessing buffer
    for (int k = 0; k < 1000; k++){
        product = 8 * 9;
    }

    //consuming items
    for (int i = 0; i < BSIZE; i++){
        num = buf[nextout++]; <---------------- ***ERROR***
        cout << "Consuming item: " << num << endl;
            // TODO: Consume item
    }

    // Unlocking thread
    pthread_mutex_unlock(&lock);

    // Exiting consumer
    pthread_exit(0);
}

我已经阅读了与此类似的其他线程,但我找不到我正在寻找的答案。感谢任何帮助,我对这种类型的编程非常陌生。

最佳答案

您将函数声明为

void * producer(void *);    // function for producer thread
void * consumer(void *);    // function for consumer thread

但是他们的定义有另一个原型(prototype):

void * producer(void * buf[]);
void * consumer(void * buf[]);

在本例中,buf 是指向 void 的指针数组。并且您正在尝试将数字分配给指向 void 的指针:

buf[nextin++] = num;

函数原型(prototype)的声明和定义必须相同。

很明显,您想要将数字写入缓冲区。但是你不能有 void 数组。因此,将 buf 转换为 int *:

static_cast<int*>(buf)[nextin++] = num;

关于c++ - 错误: invalid conversion from 'int' to 'void*' [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46534674/

相关文章:

c++ - 获取由库调用创建的窗口的窗口句柄 (HWND)

c++ - 类的位宽

c++ - VS2010编译耗时太长

c++ - 如何在没有背景的情况下加载bmp

c++ - 从 C++ 到 Objective C 调用回调函数

c++ - 什么时候调用构造函数?

c++ - 天空盒纹理未在 opengl 中显示

c++ - Rcpp 可以公开一个引用同一类的 C++ 类方法吗?

c++ - set::erase() 如何将 set::end 作为其参数?

c++ - TFTP 源代码示例