Random Access Iterator Error

I am unable to resolve the below code compilation issue:

class A
{
public:
int x;
};

void sort_A(TArray<A> &_Atype)
{
    std::stable_sort (_Atype.begin(), _Atype.end());
}

bool operator< (const A & _a1, const A & _a2)
{
 return _a1.x < _a2.x;
}

When we compile using gcc 4.1.2 on RH5.2 I am getting the below mentioned error:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function �void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]� not a class or a defined type:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3066:   instantiated from �void std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]�
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3776:   instantiated from �void std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]�
instantiated from �void sortA(TArray<A>&)
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2277: error: no match for �operator<� in �__val < * __first�

I tried all the way using {typedef , SmartPointer} to define the type but it constantly throws the above mentioned error. What and how should I should declare and define so that the type (A*) is known by compiler when calling std::stable_sort.

Please help.

Try putting the < operator inside your object instead of imposing it from outside. The function itself should be const, too, to reassure the compiler that comparing two objects won't modify their contents.

bool operator <(const A &other) const { return this.x < other.x; }

Thanks a lot. It solved my problem.

We have now a different problem in regards to implementation based upon the model above.

When we move the operator < function within the class and another flow wants to call operator < function but with a different logic - how the same can be achieved.

I guess, the operator overloading function "bool operator<", need to be declared as friend inside the class.