C ++不完整类型和转换错误

时间:2015-03-13 19:58:05

标签: c++ arrays

我正在尝试使用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,它引用每个线程应该排序的数组的边界。看起来好像我的结构实现可能不正确,但我不确定为什么不这样做。感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

您定义了struct threadData,但之后尝试使用struct thread_data类型来声明名为threadData变量

答案 1 :(得分:0)

尝试在结构中的startIndex之后删除分号(;)。

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