Вопрос

I am calling a template for finding a min out of two values The code is :

#include<iostream>
#include <cstdlib>

using namespace std;

template<class T>
T min(T a,T b)
{
    return (a<b)?a:b;
}


int main(int argc, char** argv) {
int d,y;
std::cout<<"enter two integer values";
std::cin>>d>>y;
cout<<"you entered"<<d<<y;
std::cout<<"the minimum of the two is "<<min(d,y);
float p,q;
std::cout<<"enter float values";
std::cin>>p>>q;
cout<<"you entered"<<p<<q;
std::cout<<"the minimum of the float values is "<<min(p,q);
char w,a;
std::cout<<"enter the two characters";
std::cin>>w>>a;
cout<<"you entered"<<w<<a;
std::cout<<"the minimum of the two characters is "<<min(w,a);
return 0;

}

It says call to an ovrloaded function is ambiguous

Это было полезно?

Решение

Remove

using namespace std;

because there is another min() function in std.

Другие советы

You get the error because there is a standard function called std::min defined in the namespace std, which your program allowed the compiler to use without an explicit reference.

Remove

using namespace std;

and add std:: qualifier to cout where it's missing to fix this problem.

Demo of your program compiling on ideone.

Use ::min(d,y) to indicate that you refer to the function in the global namespace.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top