1

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;
}
Community
  • 1
  • 1
Shinji-san
  • 971
  • 4
  • 14
  • 31
  • 3
    Never say `using namespace std;`. – Kerrek SB Feb 07 '18 at 02:40
  • Possible duplicate of [error: call of overloaded ‘max(int, int)’ is ambiguous](https://stackoverflow.com/questions/9097509/error-call-of-overloaded-maxint-int-is-ambiguous) – xskxzr Feb 07 '18 at 14:36

2 Answers2

4

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
1

std::max is a function defined in C++. Call your function myMax or something like that and it should work

Mitch
  • 3,342
  • 2
  • 21
  • 31