简体   繁体   中英

C++ incomplete type and conversion error

I am trying to write a c++ function using pthreads to do a sort. Receiving the following 2 errors and not sure why:

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;
        }
    }
}
}

The errors I am receiving are: cse451.cpp: In function 'void createThreads(int)': cse451.cpp:99:21: error: aggregate 'createThreads(int)::thread_data threadData' has incomplete type and cannot be defined cse451.cpp: In function 'void* bubbleSort(void*)': cse451.cpp:127:38: error: cannot convert 'bubbleSort(void*)::thread_data*' to 'threadData*' in assignment

I need threadData to contains a startIndex and stopIndex that refers to the bounds of the array that each thread should sort. It appears as though my struct implementation may not be correct but I'm not sure why not. All help is appreciated.

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

Try to remove the semicolon(;) after startIndex in the structure.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM