多制造商和类型转换

Multiple makers and type conversion

本文关键字:类型转换 制造商      更新时间:2023-10-16
Vector<Medicine*>* Controller::sortByStockAsc(){
    Vector<Medicine*>* all =repo->getAll();
    qsort(all, all->getSize(),sizeof(Medicine*), (comparefunction) compareNA);
    return all;
}

那么,上面的函数应该对对象数组进行排序。我得到错误

cannot convert 'Vector<Medicine*>' to 'Vector<Medicine*>*' in initialization

但是如果我把它写为Vector<Medicine*> all =repo->getAll();,我会在第三行得到一堆新的错误(如果我在第二行有之前的错误,就不存在了):

Multiple markers at this line
    - Method 'getSize' could not be resolved
    - Invalid arguments ' Candidates are: void qsort(void *, unsigned int, unsigned int, int (*)(const void *, const 
     void *)) '
    - base operand of '->' has non-pointer type 'Vector<Medicine*>'

这有什么问题,我怎么解决它?

试试这个:

Vector<Medicine*> Controller::getMedicinesSortedByStockAsc() {
    Vector<Medicine*> all = repo->getAll();
    std::sort(all.begin(), all.end(), compareNA);
    return all;
}

指出:

  • 你没有提供Vector的定义,所以我认为它是stl兼容的。从剩下的代码来看,这可能是一个错误的假设。
  • 你没有提供compareNA的描述,但我选择假设它可以与stl风格算法兼容。