Left & Right are functions; your code is using them as if they were arrays. To call a function, use (), not [].
Heap Sort Error: expression must be a pointer to a complete object type?
-
24-03-2022 - |
Domanda
I am implementing Heap Sort for an assignment. We have to do it the same way she did in class with her pseudocode, or else we dont get credit.
In my Max_Heapify function, im getting an error when I call my Left() and Right() functions, that say "expression must be a pointer to a complete object type". Im going directly off of psedocode, so im not sure what is causing this error, can anyone help?
void Max_Heapify(int heapArray[],int i)
{
int n = SIZE;
int largest = 0;
int l = Left[i];//<--Error here on Left
int r = Right[i];//<--Error here on Right
if(( l <= n) && (heapArray[l] > heapArray[i]))
{
largest = l;
}
else
{
largest = i;
}
if( (r <= n) && ( heapArray[r] > heapArray[largest]))
{
largest = r;
}
int temp;
if(largest != i)
{
temp = heapArray[i];
heapArray[i] = heapArray[largest];
heapArray[largest] = temp;
Max_Heapify(heapArray,largest);
}
return;
}
...........................................................................................
Here are the Left() and Right() functions if it helps at all
int Left(int i)
{
return (2*i);
}
int Right(int i)
{
return ((2*i)+1);
}
Soluzione
Altri suggerimenti
You need Left(i)
and Right(i)
, not Left[i]
and Right[i]
int l = Left[i];//<--Error here on Left
int r = Right[i];//<--Error here on Right
int l = Left(i);//should be
int r = Right(i);//should be
it is an error because we use () to espress function call in C++. you have to change
int l = Left[i];
into
int l = Left(i);
If you want to see the best possible implementation of heapsort look into the linux kernel.
http://lxr.free-electrons.com/source/lib/sort.c
I remember staring at it when I was new to C so that I could absorb perfection.