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

2
  • Why does int (*compare)(T T1, T T2) return int? Commented Nov 25, 2017 at 0:11
  • i'm working off someone elses template so I have no clue :) Commented Nov 25, 2017 at 0:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I see and I actually wrote a function called compareTerm ... that makes since
What if i wanted to pass in a value to comparTerm like comparTerm(Term,Term,int) then how would I call merge_sort?
Then you could use a functor object instead of a function, and pass extra parameters to its constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.