2012-02-18 107 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

相关问题