problem compiling program using comparison with pairs

Hi fellow c++ users. I'm puzzled why the following doesn't compile. I define
a smaller than operator (<) on complex<double> and use it in a pair. But it doesn't compile saying there is no matching <. Using my own bogus complex class it does compile. Can someone help me?

#include <cstdio>
#include <utility>

#if 1 // change to 0, to use my bogus complex class which does compile

#include <complex>

#else

template<typename T>
class complex {
T x, y;

public:
complex(T _x, T _y):x(_x), y(_y) {}

T real() const {return x;}
T imag() const {return y;}
};

#endif

using namespace std;


inline
bool operator<(const complex<double> &a, const complex<double> &b) {
return a.real()<b.real() || (a.real()==b.real() && a.imag()>b.imag());
}

int main() {
pair<int, complex<double> > a=make_pair(5, complex<double>(3,4)), b=make_pair(5, complex<double>(4,5));
if(a<b) printf("smallern");
else printf("biggern");
return 0;
}
You have to put your < operator inside the std namespace.

1
2
3
4
5
6
7
namespace std
{
    inline bool operator<(const complex<double> &a, const complex<double> &b)
    {
        return a.real()<b.real() || (a.real()==b.real() && a.imag()>b.imag());
    }
}
Hey man, mucho thx!

Topic archived. No new replies allowed.