-3

Main.cpp

#include "SelectionSort.h"

using namespace std;


int main() {

    SelectionSort<int> sorterInt;

    int test_array[20];
    sorterInt.stuffNum(&test_array, 20, 1, 200);
}

SelectionSort.h

using namespace std;

template <typename T>
class SelectionSort {
public:
    void stuffNum(T *object, int size, int min, int max)
    {
        for(int i = 0; i < size; i++)
        {


            (*object)[i] = 5;


        }
    }
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

1 Answers1

2
SelectionSort<int> sorterInt;

int test_array[20];
sorterInt.stuffNum(&test_array, 20, 1, 200);

your template have type int so your method take a int * as argument. And you write &test_array who has type int *[20] because you send the address of your array.

So just remove the &

sorterInt.stuffNum(test_array, 20, 1, 200);

You need a better understanding of pointer.

edit: (read comment)

(*object)[i] = 5;

here you should remove * and () like this

object[i] = 5;

more doc here What is array decaying?

Community
  • 1
  • 1
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • You missed one. `(*object)` will deref the pointer making `[i]` impossible. Should be `object[i]`. – user4581301 Oct 27 '16 at 05:39
  • 1
    It's not `int*[20]`, there's a missing pair of parenthesis in your answer. – StoryTeller - Unslander Monica Oct 27 '16 at 05:40
  • and his case I agree. But if he use his template with type T of int *. He could compile. – Stargateur Oct 27 '16 at 05:41
  • Wait, I think I see where you are going with that, but you're getting further off the mark of what OP's goal is likely to be. They want to sort `int`, not `int *` – user4581301 Oct 27 '16 at 05:45
  • 1
    You should mention the fact that an array decay to and bla bla bla, that is probably what confused the OP. – skypjack Oct 27 '16 at 05:56
  • That ideone link's code will blow up in your face, Stargateur. You allocate storage for the array of pointers, but none for what they point to, then dereference the pointers to uninitialized pointers and attempt dereference those uninitialized pointers and assign to the unallocated storage at them. – user4581301 Oct 27 '16 at 05:58