1.. I'm testing following code which uses template
Source code is originated from this learncpp tutorial
https://www.learncpp.com/cpp-tutorial/132-function-template-instances/
#include <iostream>
#include <cstring>
#include <typeinfo>
using namespace std;
// ======================================================================
template <typename T>
const T& max(const T& x,const T& y)
{
return (x>y)?x:y;
}
// ======================================================================
class Cents
{
private:
int m_cents;
public:
Cents(int cents):m_cents(cents)
{
}
friend bool operator>(const Cents &c1,const Cents &c2)
{
return (c1.m_cents>c2.m_cents);
}
int get_val()
{
m_cents;
}
};
// ======================================================================
int main()
{
Cents nickle(5);
Cents dime(10);
Cents bigger=max(nickle,dime);
// std::cout<<"bigger: "<<bigger.get_val()<<std::endl;
// bigger: 1471225424
return 0;
}
And I get this error
error: call of overloaded ‘max(Cents&, Cents&)’ is ambiguous
Cents bigger=max(nickle,dime);
What's wrong with the code?
2.. And how can I print result?
For example, I tried std::cout<<"bigger: "<<bigger<<std::endl;
But I ran into following error which says there is no overloaded operator << for bigger(Cent type object)
error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’
std::cout<<"bigger: "<<bigger<<std::endl;
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘Cents’)
std::cout<<"bigger: "<<bigger<<std::endl;