Вопрос

Vector<Medicine*>* Controller::sortByStockAsc(){
        Vector<Medicine*>* all =repo->getAll();
        qsort(all, all->getNrMed(),sizeof(Medicine*), (comparefunction) compareNA);
        return all;}

I have the above code and my qsort has some problems. Vector<Medicine*>* is a pointer to an array of pointers which I need to sort out. getAll() returns a Vector <Medicine*>*. One of the problems is that in qsort() my getNrMed() isn't found although I specified the module where this function is defined. The function looks like:

int MedRepository::getNrMed(){
    return MedList->getSize();
}

and returns a int. What I'm doing wrong?

Это было полезно?

Решение

qsort doesn't take a Vector<T>*. It takes a pointer to the first element of the vector.

getNrMed is not a method of Vector<T>, it's a method of MedRepository which is seen nowhere else in this snippet.

The fact that you need to put a cast on compareNA is the sign of a serious problem. It's almost never safe to cast a function pointer. You need to make it compile without a cast.

Undoubtedly, given the code quality here, there are additional issues--this is just what can be diagnosed given the limited amount of code you presented.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top