0

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
4
  • Change template <class T> void selectionsort(T* data, int size) to template <class T> void Sorting::selectionsort(T* data, int size) Commented Oct 18, 2015 at 15:59
  • Hmmm...but now I get "Sorting is not a class, namespace, or enumeration" Commented Oct 18, 2015 at 16:05
  • That std::array<T*, size> looks strange to me. Don't you mean std::array<T, size>? Do you really want an array of pointers? Commented Oct 18, 2015 at 16:10
  • @IronCode: That's doubtful. Please have a look at the latest edit of my answer. A collection of T and a collection of T* are two quite different things. Commented Oct 18, 2015 at 16:13

1 Answer 1

2
template <class T>  void selectionsort(T* data, int size)
{
    std::array<T*, size> myarray = data;

size is only known at run time. The size of an std::array has to be known at compile time.

There are two ways to fix this:

  1. Use std::vector<T*> instead of std::array<T*, size>.

  2. Make size a template argument: template <class T, int size> void selectionsort(T* data).

What's better depends on what you actually want to do.


Mind that I'm not sure that your code really does what you intend it to do. You seem to be using T* in your functions' parameters as a way to say "pointer to a number of Ts", while in your selectionsort function you are suddenly dealing with a collection of T pointers.

In other words, std::array<T*, size> (or std::vector<T*>) is a collection of T*, not a collection of T. You will probably want to use std::array<T, size> (or std::vector<T>) and then use the T* that you receive in the function to copy the T objects that the T* points to into the container. For example:

std::vector<T> myarray(data, data + size);
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh okay I see now. Sorry I struggle with the concept of pointers. But just really quick when I use a vector, I receive the error no member name 'vector' in namespace std?
@IronCode: You have to #include the correct standard-library headers. In the case of std::vector, #include <vector>.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.