设置比较器类型预期错误

发布于 2024-09-29 15:49:46 字数 625 浏览 0 评论 0原文

抱歉各位,如果这是一个愚蠢的问题。我有一组整数,我想使用函数对象对它们进行排序。但是当我尝试编译以下代码时,出现错误“'_Pr' 的模板参数无效,类型为预期”。


    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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-10-06 15:49:46

MySortedUser(_users) 是一个表达式(它创建一个 MySortedUser 对象)。该对象是在运行时构造的。模板需要在编译时实例化。

您需要将类型作为模板参数,将构造的比较器作为构造函数参数:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文