Function templates in c++?

For school, I have an assignment to create a function template and use the users input for dynamic memory. My code is not working, but my teacher says it is correct and it is just my compiler that does not work. This is the error I get from the compiler:

error: invalid conversion from 'int' to 'int*' [-fpermissive] key = array[I];

I use the compiler MinGW64. Is there anyone with the same problem who has a solution?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

template <class S>  
void sortArray(S &array, int n);

template <class R>
void reverseArray(R &array, int n);

template <class P>
void printArray(P array, int n);

int main() {
    int n1, n2, i;

    srand(time(0));

    cout << "insert how many numbers:";
    cin >> n1;
    int *nums = new int[n1];
    if(nums == 0){
        cout << "No memory allocated";
        exit(1);
    }
    for(i = 0; i < n1; i++){
        nums[i] = rand() % 100 + 1;
    }
    printArray(nums, n1);
    sortArray(nums, n1);
    printArray(nums, n1);
    reverseArray(nums, n1);
    printArray(nums, n1);
    delete []nums;

    cout << "insert how many characters:";
    cin >> n2;
    char *nums2 = new char[n2];
    if(nums2 == 0){
        cout << "No memory allocated";
        exit(1);
    }
    for(i = 0; i < n2; i++){
        nums2[i] =  rand() % 25 + 97; // rand() % 93 + 33
    }
    printArray(nums2, n2);
    sortArray(nums2, n2);
    printArray(nums2, n2);
    reverseArray(nums2, n2);
    printArray(nums2, n2);
    delete []nums2;

    return 0;
}

template <class S>
void sortArray(S &array, int n){
    int i, j;
    S key;
    for(i = 1; i < n; i++){
        key = array[i];
        for(j = i-1; (j>=0) && (array[j] > key); j--){
             array[j+1] = array[j];
        }
        array[j+1] = key;
    }
}

template <class R>
void reverseArray(R &array, int n){
    int start = 0, end = n-1;
    R temp;
    while(start < end){
        temp = array[start];
        array[start] = array[end];
        array[end] = temp;
        start++;
        end--;
    }
}

template <class P>
void printArray(P array, int n){
    int i;
    for (i = 0; i < n; ++i) {
         cout << "[" <<i << "] => " << array[i] << ", ";
    }
    cout << endl;
}

2 answers

  • answered 2018-09-19 22:14 R Sahu

    The compiler is definitely not wrong.

    There is some miscommunication.

    When you declare

    template <class S>  
    void sortArray(S &array, int n);
    

    and ust it as

    sortArray(nums, n1);
    

    S is deduced as int* given the definition of nums in your posted code. Hence, you may not use

    S key;
    for(i = 1; i < n; i++){
        key = array[i];
    

    since key is of type int* while array[i] is of type int.

    You can fix the problem by using one of the following two options.

    1. Change the function declaration to

      template <class S>  
      void sortArray(S* array, int n);
      
    2. Change the type of key.

      typename std::remove_reference<decltype(*array)>::type key;
      

      or let the compiler deduce it using auto.

      auto key = array[0];
      

      The downside of using the second method is that the program will have undefined behavior if array is empty.

    You'll have to change reverseArray similarly.

  • answered 2018-09-20 12:50 Bolt123

    Thank you for all the information. My teacher wouldn't help me because he was convinced it was my compiler.
    So all I had to do was chancing my function declaration to void sortArray(S *array, int n); and now it works perfectly.