[英]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.