mexx
mexx

Reputation: 3

set comparator type expected error

Sorry guys, if this is a stupid question. I have a set of integers which I want be sorted using a function object. But when I try to compile the following code, I get the error "invalid template argument for '_Pr', type expected".


    class MySortedUser{
    vector < user* > & users;
    public:
 MySortedUser(vector < user* > & _users):users(_users)
 {
 }
 bool operator()(const int& A, const int& B) const
 {
  return (users[A]->username < users[B]->username);
 } 
   };
    void someFunction(vector < user* > & _users)
    {
      set< int, MySortedUser(_users) > MySet;  //error here
    }

Can you please tell me what am I doing wrong.

Upvotes: 0

Views: 322

Answers (1)

James McNellis
James McNellis

Reputation: 355207

MySortedUser(_users) is an expression (it creates a MySortedUser object). The object is constructed at run time. Templates need to be instantiated at compile time.

You need to give std::set the type as a template argument and the constructed comparator as a constructor argument:

std::set<int, MySortedUser> MySet(MySortedUser(_users));

Upvotes: 1

Related Questions