Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7531661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:13:56+00:00 2026-05-30T05:13:56+00:00

I cannot sort the list which contains the pointers to objects of my template

  • 0

I cannot sort the list which contains the pointers to objects of my template class.
template

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;
    //...
};

outside the class:

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_;
    }

}

and then I want to sort before print, I have

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()){
    //...
    }
} 

errors:

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> *)
          ]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T05:13:58+00:00Added an answer on May 30, 2026 at 5:13 am

    std::sort requires random-access iterators, so it cannot be used with std::list iterators.

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

    Make sure you read the documentation for template functions carefully. Template errors can be a nightmare to deal with.

    Edit:
    As Christian mentioned, std::list has its own sort method. You could just make these two changes:

    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);

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a list containing a custom class, MyClass MyClass has properties, which
I have a list of objects which all have an id property E.g 1,
How to sort list based on its element value? In my unit test class,
I'm wanting to sort a large array of Strings (notably File.list() , which I
I have this Player class which implements the Comparable interface. Then I have an
I have a list which stores a lost of integers. I don't like the
The list sort() method is a modifier function that returns None . So if
I came across a scenario where I need to sort a list of custom
Possible Duplicate: Sort a list of tuples by their second elements Hey there i
I need something exactly like a SQL JOIN which cannot be done with appengine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.