0

I have a custom comparator and I pass in two const references of a custom struct to be compared, however, I get the following error:

'bool cmp::operator()(const LogObjects &, const LogObjects &)' cannot convert argument 2 from 'const_Ty' to 'const LogObjects &'.

I've tried adding and removing const, and references but it didn't work.

bool cmp::operator()(const LogObjects &a, const LogObjects &b) { // body }

struct LogObjects {
    string category;
    string message;
    string timestamp;
    long long int time;
    int entry_id;

sort(master.begin(), master.end(), cmp());
auto it1 = lower_bound(master.begin(), master.end(), time, cmp());
auto it2 = upper_bound(master.begin(), master.end(), time, cmp());

};

(master is a vector of LogObjects and time is a long long int)

rpiston
  • 3
  • 4
  • can you add the part of code that's using the comparator? – David Mar 27 '19 at 15:02
  • without that part the only thing I can think of is that you should change your cmp::operator() to be a const member – David Mar 27 '19 at 15:05
  • From the error message, my guess would be that you are trying to use `cmp` to compare objects of some type that is not in fact `LogObjects` – Igor Tandetnik Mar 27 '19 at 15:13
  • 1
    The code you've shown [compiles](https://rextester.com/DICTGH17358). To the extent there is a problem, it must needs lie in the code you haven't shown. Prepare a [mcve] – Igor Tandetnik Mar 27 '19 at 15:16
  • "time is a long long int" << this is why you are getting the error. It's in lower_bound, upper_bound. the functions try to compare item in your vector( of type LogObject) with time (of type long long). Easiest way to fix would be to create LogObject timeObj and pass that to lower/upperBound. (If cmp depends on time only) – David Mar 27 '19 at 15:31

1 Answers1

0

Just so the question can be closed, answering here as well.

time is a long long int

Is why you are getting the error. lower_bound and upper_bound try to compare item in your vector( of type LogObject) with time (of type long long). Easiest way to fix would be to create LogObject timeObj and pass that to lower/upperBound. (If cmp depends on time only)

something like this:

LogObjects timeObject;
timeObject.time = time;
auto it1 = lower_bound(master.begin(), master.end(), timeObject, cmp());
auto it2 = upper_bound(master.begin(), master.end(), timeObject, cmp());
David
  • 602
  • 5
  • 14