There is a template function called insertionSort defined as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template < class Comparable >
void insertionSort ( vector < Comparable > & a )
{
for ( int p = 1; p < a.size ( ); p++ )
{
Comparable tmp = a [ p ];
int j;
for ( j = p; j > 0 && tmp > a [ j - 1 ]; j-- )
a [ j ] = a [ j - 1 ];
a [ j ] = tmp;
}
}
There is a abstract class called shapes and several derived classes who implement the virtual functions within shape. Now if I want to sort an array of Shape Objects based on area of the objects, I cannot pass an array of Shape because this would just do insertion sort on pointers.
So I create a struct which is used to dereference pointers to Shape.
This gives me a warning : signed/unsigned mismatch inside insertionSort at the first for statement and a whole lot of errors after that ( probably caused due to this error ).
Now my question is: Is this warning caused because I am passing an array of PtrToShape to the template variable Comparable? Or when you pass an array to Comparable (the template variable), do you need to do anything?
The signed/unsigned warning is because vector::size returns a size_t, which is an unsigned int. You get rid of it by declaring p and j as size_t instead of int.
However the reason why it won't work is probably because PtrToShape is not comparable. Did you mean to write operator< instead of operator= in the PtrToShape class?