开发者

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:
 开发者_JAVA百科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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜