I am trying to do a project for an algorithms class that requires implementing different sorting algorithms. I am trying to declare an array using the template class as shown. I have to keep the function definitions the same and therefore cannot change any of the parameters. My question is for my array declaration, I get the error "Non type template argument is a non constant expression." What is the correct way to declare a template array? Any help will be appreciated.
#ifndef __SORTING_HPP
#define __SORTING_HPP
#include "SortingHelper.h"
#include <iostream>
template <class T>
class Sorting
{
public:
T selectionsort(T* data, int size);
T insertionsort(T* data, int size);
T mergesort(T* data, int size, T* temp);
T quicksort(T* data, int size);
T data;
};
template <class T> void selectionsort(T* data, int size)
{
std::array<T*, size> myarray = data;
int min = 0;
int temp = 0;
if (isSorted(data, size))
{
return *data;
}
else
{
for (int i=0; i < size - 1; i++)
{
min = i;
for (int j=i+1; j < size; j++)
{
if (data[j] < data[min])
min= j;
}
if (min != i)
{
temp = data[i];
data[i] = data[min];
data[min] = temp;
}
}
}
}
#endif
std::array<T*, size>looks strange to me. Don't you meanstd::array<T, size>? Do you really want an array of pointers?Tand a collection ofT*are two quite different things.