Hello, everyone Im writing a simple program where the user will fill an array of integers. The array is then passed to a sort function that I'm writing. Heres the function prototype and function call.
1 2 3 4 5 6
//prototype:
void sortArray(int sizeOfArray, int &myArray);
//function call in main()
sortArray(sizeOfArray, &myArray)
When compiling I'm getting these errors:
1) error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int (*)[(((sizetype)(((ssizetype)sizeOfArray) + -1)) + 1)]'|
2)error: in passing argument 2 of 'void sortArray(int, int&)'|
I've tried to search for a solution but all changes I've made only seemed to create more errors.
In your prototype you declared my array as a reference to single int. But you are trying to pass a pointer to int. You probably want to declare myArray as pointer.
If you are using a lot of different sizes of array, the template aproach could lead to code bloat. So could implement the template version using the pointer version.
Understand. Ill make the corrections suggested. I do have a question though. The size of the array is not known at compile time. I assume that something like void sortArray(int (&myArray)[sizeOfArray]) is illegal. Is there another way of passing the array size to the function? Or should I just declare a constant that holds the array size?