I keep getting an error “no instance of overloaded function “printArray” matches the argument list. Will someone please tell me why? I’m trying to overload a template function so that it displays elements of an array starting and ending at specified positions.
I have my initial template and function:
template< typename T >
void printArray( const T *array, int count )
And the function that I’m trying to overload.
template< typename T >
void printArray(int lowSubscript, int highSubscript)
and my call:
// display elements 1-3 of array a
cout << "Array a from positions 1 to 3 is:\n";
elements = printArray(1,3);
my call for the first printArray shows no errors:
// display array a using original printArray function
cout << "\nUsing original printArray function\n";
printArray( a, ACOUNT );
Your second overload of
printArrayis a template, but the signature is not dependent on the template parameterT. Therefore you must specify it when calling, e.g.printArray<int>(1,3).However, it is unclear which array is being printed: did you forget to add a parameter for the array, e.g.
printArray(a,1,3)? In which case, you might be able to haveTdeduced from that parameter, as it is in theprintArray(a,count)version.Also, the return type of
voidmeans you cannot writeelements=printArray(1,3)even if the template parameter could be deduced.