Answer the question
In order to leave comments, you need to log in
Why does this template function not want to be reloaded?
#include<iostream>
using namespace std;
template < typename T > void Function1(T a, T b);
void main() {
int i;
double d;
char c;
int b;
cout << "Enter int number: ";
cin >> i;
Function1(i, b);
cout << endl;
cout << "Enter double number: ";
cin >> d;
Function1(d, b);
cout << endl;
cout << "Enter char number: ";
cin >> c;
Function1(c, b);
cout << endl;
}
template < typename T >
void Function1(T a, T b = 2) {
T c;
for (int i = 0; i <= b; ++i) {
c = a*a;
}
cout << c;
}
Answer the question
In order to leave comments, you need to log in
I will not paint all the problems of this code, I will focus on the main thing:
Function1 is declared as taking 2 arguments of the same type . In the second and third cases, you call it by slipping arguments of different types. Accordingly, the compiler cannot decide what T will be in this situation - double or int, char or int? Either make 2 template types, or make the 2nd argument non-template, or pass arguments of the same type, or explicitly tell the function when calling what T
is . , in the second - no.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question