I am getting this error and another error too ** "IntelliSense: no instance of function template matches the argument list"** when compiling the following code
I know there might be logic mistakes in my function but I need to solve this error first to be able to debug my function .
#include <iostream> using namespace std; template<class T> T myMax (T& arr ,int arrStart ,int arrSize) { if(arrStart==arrSize-1) return arr[arrSize]; int median = (arrStart+arrSize)/2 ; T left , right , maximum ; left = max(arr,arrStart , median); right = max(arr , median+1 , arrSize-1) ; if (left>right) maximum = left; else maximum = right ; return maximum ; } void main() { int arrSize = 5; int arr[] = {1,3,4,6,22}; int x; x = myMax(arr,0,arrSize); }
mainmust beint.arr[arrSize];is incorrect because it accesses an element that doesn't exist (arr[arrSize]is one past the end of the array). Since the function doesn't modifyarr, if you do choose to take the array by reference, it should be a const reference.