2013-05-07 53 views
-3
Vector<Medicine*>* Controller::sortByStockAsc(){ 
    Vector<Medicine*>* all =repo->getAll(); 
    qsort(all, all->getSize(),sizeof(Medicine*), (comparefunction) compareNA); 
    return all; 
} 

所以,我有以上这应该排序objects.I阵列功能得到错误多个制造商和类型转换

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*>' 

什么不对的,我该如何解决?

+0

'repo-> getAll()'返回什么? – stardust 2013-05-07 10:36:31

+1

很多事情都是错误的,我甚至从哪里开始?你有一个函数,听起来像一条指令去做某件事,但然后返回一些东西。你使用qsort。你投了一个函数指针。你不明白指针,引用和值之间的区别。你有一个指针向量,你也会返回一个(或者甚至是一个指向向量指针的指针),这从所有权角度来看是完全混淆的。基本上,问题是你不知道C++。为了解决这个问题,我建议你阅读一本好书。 – 2013-05-07 10:36:56

+0

@命名为Vector的所有对象Vector Matt 2013-05-07 10:45:23

回答

1

试试这个:

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

注:

  • 你没有提供的Vector的定义,所以我认为它是STL兼容。从其他代码来看,这可能是一个不正确的假设。
  • 您没有提供compareNA的描述,但我选择假定它可以与STL样式算法兼容。
+0

'class Vector { private: \t T * Elems; \t int Size; \t int Capacity;' ... +构造函数,析构函数,getters。 – Matt 2013-05-07 13:43:59

+0

int compareNA(const Medicine * e1,const Medicine * e2){ \t int q1 =(*(Medicine **)e1) - > getStock(); \t int q2 =(*(Medicine **)e2) - > getStock(); \t if(q1 Q2){ \t \t \t返回1; \t \t} \t \t return 0; \t} }' – Matt 2013-05-07 13:44:24

+0

我tryed你的版本,但我得到这个错误:在该行 _Multiple标记 \t - 参数无效“考生:无效的排序(#0,#0)无效的排序(#0, \t#0,#1)' \t - 方法“开始”无法解析 \t - 方法'结束'无法解决_ – Matt 2013-05-07 13:46:11