簡體   English   中英

錯誤:從'int'到'void *'的無效轉換[-fpermissive]

[英]error: invalid conversion from 'int' to 'void*' [-fpermissive]

為實驗室構建一個程序,我必須使用線程,我對此有點迷惑,但是我接近將其編譯。 我有2個錯誤:標題中提到了一個錯誤,另一個錯誤是相同的,但是它表示從'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

但是他們的定義還有另一個原型:

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

在這種情況下, buf是指向void的指針數組。 而且您正在嘗試將數字指向指向void指針:

buf[nextin++] = num;

函數原型的聲明和定義必須相同。

顯然,您想將數字寫入緩沖區。 但是您不能具有void的數組。 因此將bufint *

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM