2015-03-13 62 views
1

我想用pthreads編寫一個C++函數來進行排序。接受以下2個錯誤,不知道爲什麼:C++不完整類型和轉換錯誤

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:在功能 '無效createThreads(INT)': cse451.cpp:99:21:錯誤:骨料'createThreads(INT):: thread_data threadData具有不完整的類型和不能被定義 cse451.cpp:在函數 'void *冒泡(無效*)': cse451.cpp:127:38:錯誤:不能轉換「冒泡(void *):: thread_data *'到'threadData *'在作業中

我需要threadData來包含startIndex和stopIndex,它指向數組的邊界ch線程應該排序。看起來好像我的結構實現可能不正確,但我不確定爲什麼不。所有的幫助表示讚賞。

+1

什麼是thread_data,它在哪裏定義? – P0W 2015-03-13 20:02:42

回答

1

你定義struct threadData,但隨後試圖用型struct thread_data聲明一個變量命名threadData

+0

照顧了一個錯誤。仍然收到cse451.cpp:99:21:錯誤:聚合'createThreads(int):: thread_data threadData'具有不完整的類型,無法定義 – GregH 2015-03-13 20:07:46

+0

該錯誤表明您沒有修復每個問題的發生,但沒有看到什麼變化我不能肯定地說。 – 2015-03-13 20:36:20

+0

是的,發現了錯誤的其他發生。 thankk你 – GregH 2015-03-13 20:49:45

0

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

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

不小心的錯誤:cse451.cpp:99:21:錯誤:總'createThreads(INT):: thread_data threadData具有不完整的類型,不能被定義 – GregH 2015-03-13 20:27:59

+0

請更改結構thread_data threadData;結構化threadData dataxyz; – JackChen255 2015-03-13 20:54:20