0

So I am writting a code where I am sorting something. And I have my definition in a class term.cpp

friend bool operator<(Term T1, Term T2);

Then in a Template I was provided with we have a implementation of the merge sorting algorithm as

void SortingList<T>::merge_sort(int (*compare)(T T1, T T2));

Now suppose I have the following

SortingList<Term> randomList;
randomList.merge_sort(???);

So my question is what do I put in the ??? I tried

randomList.merge_sort(Term::operator<(Term T1, Term T2));

but it did not work and i'm confused, I do not know how the compiler wants the operator put inside the merge_sort implementation

Riemann-bitcoin.
  • 71
  • 1
  • 2
  • 11

1 Answers1

2

You would have to create a compare wrapper if the requirement is to return an int. This would convert the bool returned from operator<

int compareTerm(Term T1, Term T2)
{
   if (T1 < T2)
      return -1;
   else if (T2 < T1)
      return 1;
   else
      return 0;
}

Then just pass compareTerm to merge_sort.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • I see and I actually wrote a function called compareTerm ... that makes since – Riemann-bitcoin. Nov 25 '17 at 13:47
  • What if i wanted to pass in a value to comparTerm like comparTerm(Term,Term,int) then how would I call merge_sort? – Riemann-bitcoin. Nov 25 '17 at 14:05
  • 1
    Then you could use a [`functor object`](https://stackoverflow.com/questions/356950/c-functors-and-their-uses) instead of a function, and pass extra parameters to its constructor. – Bo Persson Nov 25 '17 at 14:15