cppcoder
cppcoder

Reputation: 23095

Error in template function

What is the problem with this code?

#include <iostream>
using namespace std;

template <typename T>
T max(T X, T Y)
{
    return (X > Y) ? X : Y;
}

int main()
{
    int x = max(5,6);
}

I am getting this error:

overload.C: In function 'int main()':
overload.C:19: error: call of overloaded 'max(int, int)' is ambiguous
overload.C:12: note: candidates are: T max(T, T) [with T = int]
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2  /bits/stl_algobase.h:206: note:                 const _Tp& std::max(const _Tp&, const _Tp&) [with _Tp = int]

Upvotes: 3

Views: 110

Answers (2)

Israel Unterman
Israel Unterman

Reputation: 13510

Perhaps max is already declared by the standrad library. Try not importing the namespace.

Upvotes: 0

KCH
KCH

Reputation: 2844

max is already defined in standard library. Remove using namespace std and it should work.

Upvotes: 12

Related Questions