2012-02-18 103 views
1

我無法對包含指向我的模板類的對象的指針的列表進行排序。 模板list <myClass<int> *> sort

class Node{ 
public: 
    Node(void){} 
    Node(T choice, vector<T> & data); 
    Node(T choice, vector<T> & data, player p, int level, int pos, Node<T> *pred, which side); 
    void addNodes(void); 
    static list<Node<T> * > Nodes; 
    friend class MyBinomialTree<Node<T>, T>; 
    bool sorter(const Node<T> * lhs, const Node<T> * rhs);// as * &lhs doesn't work too 
private: 
    Node<T> * left; 
    Node<T> * right; 
    T chosen_; 
    vector<T> data_; 
    bool isLeaf; 
    //... 
}; 

類外:

template<class T> 
bool Node<T>::sorter(const Node<T> * lhs, const Node<T> * rhs){ 
    if((*lhs).level_==(*rhs).level_){ 
     return (*lhs).pos_<(*rhs).pos_; 
    }else{ 
     return (*lhs).level_<(*rhs).level_; 
    } 

} 

,然後我想打印前進行排序,我有

template<class T> 
void Node<T>::print(void){ 

    std::sort(Node<T>::Nodes.begin(),Node<T>::Nodes.end(),&Node<T>::sorter); 
    cout<<endl<<"after sort:"<<endl; 

    list<Node<T> * >::iterator it=Node<T>::Nodes.begin();cout<<endl; 
    while(it!=Node<T>::Nodes.end()){ 
    //... 
    } 
} 

錯誤:

c:\program files\microsoft visual studio 10.0\vc\include\algorithm(3806): error C2784: 'std::complex<_Other> std::operator -(const _Ty &,const std::complex<_Other> &)' : could not deduce template argument for 'const std::complex<_Other> &' from 'std::_List_iterator<_Mylist>' 
      with 
      [ 
       _Mylist=std::_List_val<Node<int> *,std::allocator<Node<int> *>> 
      ] 
      c:\program files\microsoft visual studio 10.0\vc\include\xcomplex(52) : see declaration of 'std::operator -' 
      c:\documents and settings\cf16r\moje dokumenty\visual studio 2010\projects\binomial_tree\binomial_tree\mybinomialtree.h(136) : see reference to function template instantiation 'void std::sort<std::_List_iterator<_Mylist>,bool(__thiscall Node<T>::*)(const Node<T> *,const Node<T> *)>(_RanIt,_RanIt,_Pr)' being compiled 
      with 
      [ 
       _Mylist=std::_List_val<Node<int> *,std::allocator<Node<int> *>>, 
       T=int, 
       _RanIt=std::_List_iterator<std::_List_val<Node<int> *,std::allocator<Node<int> *>>>, 
       _Pr=bool (__thiscall Node<int>::*)(const Node<int> *,const Node<int> *) 
      ] 
+0

試圖聲明'布爾sorter'類的靜態功能。 – qehgt 2012-02-18 22:52:34

+0

這是我的(至少暫時的,因爲它不是模板)解決方案:BOOL分揀機(常量節點 * LHS,常量節點 * RHS){ \t如果((*左).LEVEL _ ==(* RHS).level_ ){ \t \t return(* lhs).pos _ <(* rhs).pos_; \t} else { \t \t return(* lhs).level _ <(* rhs).level_; \t} } 空隙sortNodes(無效){ \t //的typedef BOOL(* comparer_t)(常量節點 *,常量節點 *); \t //比較器_t cmp =&sorter; \t節點 :: Nodes.sort(sorter); }; – 4pie0 2012-02-19 00:53:30

+0

'std :: list'有一個內置的排序函數,也是一個函數作爲參數,也許這對你有用。 – 2012-02-19 09:44:05

回答

3

std::sort要求隨機訪問迭代器,因此它不能與迭代器一起使用。

http://msdn.microsoft.com/en-us/library/ecdecxh1(v=vs.100).aspx

請務必閱讀仔細模板函數的文檔。模板錯誤可能是一個噩夢來處理。

編輯:
正如基督教提到的,std::list有它自己的排序方法。你可以只讓這兩個變化:

bool sorter(const Node<T> * lhs, const Node<T> * rhs);
static bool sorter(const Node<T> * lhs, const Node<T> * rhs);

std::sort(Node<T>::Nodes.begin(),Node<T>::Nodes.end(),&Node<T>::sorter);
Nodes.sort(&Node<T>::sorter);

+2

+1「'模板錯誤可以是一個噩夢來處理。」' – Qix 2012-09-18 07:05:53

+0

這個答案只是修復了我正在處理的模板錯誤,感謝您結束我的噩夢! – Contango 2013-06-11 12:27:33

相關問題