Reputation: 1001
I am new to templates and I am trying to test this out after reading up on how to do templates, but I'm getting a compilation error and my code looks the exact same as the example I took from. I create a function and I have a template, but when I compile it I get the following error:
15 20 C:\Users\Fire\Desktop\test.cpp [Error] call of overloaded 'max(int, int)' is ambiguous
15 20 C:\Users\Fire\Desktop\test.cpp [Note] candidates are:
7 3 C:\Users\Fire\Desktop\test.cpp [Note] T max(T, T) [with T = int]
The code is as follows:
#include <iostream>
using namespace std;
template<typename T>
T max(T a, T b){
return (a > b)? a: b;
}
int main(){
cout << max<int>(10, 40);
return 0;
}
Upvotes: 1
Views: 678
Reputation: 33944
std::max
is part of the std
namespace. You are doing using namespace std
and resolving all functions without the std
qualifier. This means you have 2 versions of max
in your code. Yours and namespace std
. To solve this, never do using namespace std
.
Upvotes: 4
Reputation: 3416
std::max is a function defined in C++. Call your function myMax or something like that and it should work
Upvotes: 1