error en plantilla funcion
Publicado por luis (1 intervención) el 18/10/2006 10:22:51
Estoy utilizando el siguiente codigo sencillo para plantillas de funciones:
#include <cstdlib>
#include <iostream>
using namespace std;
template<class T> T min(T a, T b)
{
if (a > b)
return a;
else
return b;
}
int main(int argc, char *argv[])
{
int x=11, y=5;
int res=0;
res=min(x, y);
cout << res << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Pero al compilar me produce el siguiente error:
Linea Unidad Mensaje
------------------------------------------------------------------------------------
C:\ProyC++\PlantFunc01\main.cpp In function `int main(int, char**)':
18 C:\ProyC++\PlantFunc01\main.cpp call of overloaded `min(int&, int&)' is ambiguous
note C:\ProyC++\PlantFunc01\main.cpp:7 candidates are: T min(T, T) [with T = int]
note C:\ProyC++\PlantFunc01\main.cpp:7 const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = std::streamsize]
C:\ProyC++\PlantFunc01\Makefile.win [Build Error] [main.o] Error 1
Esto trabajando con la version 4.9.9.2 de Dev-C++
Una solucion ha sido poner parametros por referencia en la funcion de la plantilla:
template<class T> T min(T& a, T& b)
De esta forma sí funciona pero no entiendo por qué al principio da error.
Gracias y salu2
#include <cstdlib>
#include <iostream>
using namespace std;
template<class T> T min(T a, T b)
{
if (a > b)
return a;
else
return b;
}
int main(int argc, char *argv[])
{
int x=11, y=5;
int res=0;
res=min(x, y);
cout << res << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Pero al compilar me produce el siguiente error:
Linea Unidad Mensaje
------------------------------------------------------------------------------------
C:\ProyC++\PlantFunc01\main.cpp In function `int main(int, char**)':
18 C:\ProyC++\PlantFunc01\main.cpp call of overloaded `min(int&, int&)' is ambiguous
note C:\ProyC++\PlantFunc01\main.cpp:7 candidates are: T min(T, T) [with T = int]
note C:\ProyC++\PlantFunc01\main.cpp:7 const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = std::streamsize]
C:\ProyC++\PlantFunc01\Makefile.win [Build Error] [main.o] Error 1
Esto trabajando con la version 4.9.9.2 de Dev-C++
Una solucion ha sido poner parametros por referencia en la funcion de la plantilla:
template<class T> T min(T& a, T& b)
De esta forma sí funciona pero no entiendo por qué al principio da error.
Gracias y salu2
Valora esta pregunta


0