我無法對包含指向我的模板類的對象的指針的列表進行排序。 模板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> *)
]
試圖聲明'布爾sorter'類的靜態功能。 – qehgt 2012-02-18 22:52:34
這是我的(至少暫時的,因爲它不是模板)解決方案: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
'std :: list'有一個內置的排序函數,也是一個函數作爲參數,也許這對你有用。 – 2012-02-19 09:44:05