-6

I have some problems to compile this code. I cannot find out the mistakes. The error is not because of the scope or the constant function. I also tried to put the sort method in a non-constant function and the same error occurred.

struct _Invoice {
    unsigned int amm;
    string id;
};
.
.
.    
vector<_Invoice> Invoices;
.
.
.
bool invComp(const _Invoice &a, const _Invoice &b){
    return a.amm < b.amm;
}
unsigned int  MedianInvoice  ( void ) const{
    vector<_Invoice>tmpInvoices(Invoices);
    sort(tmpInvoices.begin(), tmpInvoices.end(), invComp);
    return (tmpInvoices.begin() + ceil((double)tmpInvoices.size() / 2))->amm;
}

Thanks in advance!

totalolage
  • 27
  • 8
  • 2
    You forgot to show us your `invComp` function, which is the single most important thing here. You also neglected to say what error you saw, what your input is, what your expected output is, what you actually got, etc. We need an [MCVE] to even begin helping. – ShadowRanger Mar 25 '17 at 01:53
  • Off topic: [What are the rules about using an underscore in a C++ identifier?](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Hint: You're breaking them. – user4581301 Mar 25 '17 at 02:00
  • Thanks, I wasn't aware of those rules. I've only been using c++ for a month or two. Also invCmp function is there now. – totalolage Mar 25 '17 at 07:02

1 Answers1

2

 1. You need to provide invComp for us to see if there is something wrong.

 2. You'd want to use nth_element instead of sort for this task.

auto const n = static_cast<std::size_t>(std::ceil(tmpInvoices.size() / 2.0));
std::nth_element(tmpInvoices.begin(), tmpInvoices.begin() + n, tmpInvoices.end(), invComp);
return (tmpInvoices.begin() + n)->amm;
Pixelchemist
  • 24,090
  • 7
  • 47
  • 71
  • Thanks, I totally forgot to include the comp function. It should be there now, thanks again for the help and sorry. – totalolage Mar 25 '17 at 07:01
  • @FilipKalny: You'd also need to include the specific error/problem. What is it that goes wrong? The comparison function looks correct. – Pixelchemist Mar 25 '17 at 14:39
  • The compiler output was ridiculously long, that is why I did not include it, it had to do with the inner workings of the sort function. I've found the error now, I had no idea that a method and function are two completely different things, my compare function was in fact a method. – totalolage Mar 26 '17 at 05:16