簡體   English   中英

C ++不完整的類型和轉換錯誤

[英]C++ incomplete type and conversion error

我正在嘗試使用pthreads編寫c ++函數來進行排序。 收到以下2個錯誤,但不確定原因:

struct threadData{
int thread_id;
int stopIndex;
int startIndex;
};

void createThreads(int k){
struct thread_data threadData;

int numThreads = k;
int i = 0;
int err = 0;
pthread_t *threads = static_cast<pthread_t*>(malloc(sizeof(pthread_t) * numThreads));
for(i = 0;i<numThreads;i++){
    threadData[i].thread_id = i;
    //start and stop are 1 indexed
    if(i==0){
        threadData[i].start = ((N/k)*i)+1;
    }
    else{
        threadData[i].start = ((N/k)*i);
    }
    threadData[i].stop = ((N/k)* (i+1));

    err = pthread_create(&threads[i], NULL, bubbleSort, (void *)&threadData[i]); // replace foo with bubbleSort()
    if(err != 0){
        printf("error creating thread\n");
    }
}
}

void *bubbleSort(void *threadArg){
struct threadData *threadData;
threadData = (struct thread_data *) threadArg;
printf("debug\n");
bool sorted = 0;
int x;
while(!sorted){
    int start = threadData->startIndex; 
    int stop = threadData->stopIndex;

    sorted = 1;
    for(x = num_array[start]; x < num_array[stop-1]; x++){
        if(num_array[x] > num_array[x+1]){
            int temp = num_array[x+1];
            num_array[x+1] = num_array[x];
            num_array[x] = temp;
            sorted = 0;
        }
    }
    if(sorted){
        break;
    }
    sorted = 1;
    for(x = stop; x > start+1; x--){
        if(num_array[x-1] > num_array[x]){
            int temp = num_array[x-1];
            num_array[x-1] = num_array[x];
            num_array[x] = temp;
            sorted = 0;
        }
    }
}
}

我收到的錯誤是:cse451.cpp:在函數'void createThreads(int)'中:cse451.cpp:99:21:錯誤:聚合'createThreads(int):: thread_data threadData'類型不完整,無法定義cse451 .cpp:在函數'void * bubbleSort(void *)'中:cse451.cpp:127:38:錯誤:無法在分配中將'bubbleSort(void *):: thread_data *'轉換為'threadData *'

我需要threadData包含一個startIndex和stopIndex,它們引用每個線程應排序的數組的范圍。 看來我的結構實現可能不正確,但我不確定為什么不正確。 感謝所有幫助。

您定義了struct threadData ,但隨后嘗試使用struct thread_data類型聲明一個名為threadData變量

嘗試在結構中的startIndex之后刪除分號(;)。

struct threadData{
int thread_id;
int stopIndex;
int startIndex
};

暫無
暫無

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

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