设置比较器类型预期错误
抱歉各位,如果这是一个愚蠢的问题。我有一组整数,我想使用函数对象对它们进行排序。但是当我尝试编译以下代码时,出现错误“'_Pr' 的模板参数无效,类型为预期”。
Can you please tell me what am I doing wrong.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 }
<代码>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MySortedUser(_users)
是一个表达式(它创建一个MySortedUser
对象)。该对象是在运行时构造的。模板需要在编译时实例化。您需要将类型作为模板参数,将构造的比较器作为构造函数参数:
MySortedUser(_users)
is an expression (it creates aMySortedUser
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: