vendredi 9 août 2019

Why does this code have no match for operator error?

I had a encountered this question in a test and am not sure why exactly does it produce an error.

#include <iostream>
#include <string.h>

using namespace std;

template<typename T>
T Min(T a, T b)
{
    if(a<=b)
        return a;
    else
        return b;
}

class A
{
public:
    int n;
    A(int n=0) : n(n) {}
};

int main()
{
  A a1(2), a2(1);
  cout << Min(a1,a2).n;
  return 0;
}

I tried typing it out and running it and one of error messages was this.

error: no match for 'operator<=' (operand types are 'A' and 'A')

Why is that so? Would appreciate if anyone could explain, thanks!

Aucun commentaire:

Enregistrer un commentaire