排序函数不适用于在堆栈上创建的函数对象?
#include<iostream>
#include<vector>
#include<algorithm>
class Integer
{
public:
int m;
Integer(int a):m(a){};
};
class CompareParts
{
public:
bool operator()(const Integer & p1,const Integer & p2)
{
return p1.m<p2.m;
}
}obj1;
int main()
{
std::vector<Integer> vecInteger;
vecInteger.push_back(Integer(12));
vecInteger.push_back(Integer(13));
vecInteger.push_back(Integer(5));
vecInteger.push_back(Integer(7));
vecInteger.push_back(Integer(9));
Integer obj2();
std::sort(vecInteger.begin(),vecInteger.end(),obj1);
std::sort(vecInteger.begin(),vecInteger.end(),obj2);
}
为什么 obj2 在第二个排序函数中会导致编译器错误。
#include<iostream>
#include<vector>
#include<algorithm>
class Integer
{
public:
int m;
Integer(int a):m(a){};
};
class CompareParts
{
public:
bool operator()(const Integer & p1,const Integer & p2)
{
return p1.m<p2.m;
}
}obj1;
int main()
{
std::vector<Integer> vecInteger;
vecInteger.push_back(Integer(12));
vecInteger.push_back(Integer(13));
vecInteger.push_back(Integer(5));
vecInteger.push_back(Integer(7));
vecInteger.push_back(Integer(9));
Integer obj2();
std::sort(vecInteger.begin(),vecInteger.end(),obj1);
std::sort(vecInteger.begin(),vecInteger.end(),obj2);
}
why is obj2 in second sort function leads to compiler error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Integer obj2()
不是对象的定义,它是一个名为obj2
的函数的声明,返回一个Integer
(将其放在外面任何函数都可以理解为什么会这样)。有时,这种情况也会发生在更复杂的结构中,这可能会更加令人困惑。有人称这是最令人烦恼的解析。这是一个更复杂情况的承诺示例:
这里
quxx
是一个返回 Bar 的函数,并采用(指针)指向返回 Foo 且不带参数的函数。您可以更清楚地编写相同的声明,如下所示:要获取使用采用 Foo 的构造函数初始化的变量的定义,您可以添加一层括号:
Integer obj2()
isn't the definition of an object, it is the declaration of a function namedobj2
returning anInteger
(put it outside any function to understand why it is so). This occurs also sometimes with more complex constructions where it can be even more confusing. Some name this the most vexing parse.Here is the promised example of a more complex case:
Here
quxx
is a function returning a Bar and taking (a pointer) to a function returning a Foo and without parameters. You could write the same declaration more clearly like this:To get the definition of a variable initialized with the constructor taking a Foo, you can add a level of parenthesis:
因为 obj2 是一个函数。请参阅此
Because obj2 is a function. See this
obj2 不是 BinaryPredicate,并且无效,因为 std::sort
obj2 的第三个参数需要类似于
obj1 使用的函子类型。
obj2 is not a BinaryPredicate and is invalid as the third parameter to std::sort
obj2 needs to be something like
or the functor type used by obj1.
没有无参数构造函数的定义。
使用,整数 obj2(0);
There is no definition of no argument constructor.
Use, Integer obj2(0);
整数类
{
公众:
int m;
整数(int a):m(a){};
};
比较零件类
{
公众:
bool 运算符()(const Integer & p1,const Integer & p2)
{
返回 p1.m
}
};
int main()
{
std::向量 vecInteger;
vecInteger.push_back(整数(12));
vecInteger.push_back(Integer(13));
vecInteger.push_back(Integer(5));
vecInteger.push_back(Integer(7));
vecInteger.push_back(整数(9));
}
输出:
5 7 9 12 13
class Integer
{
public:
int m;
Integer(int a):m(a){};
};
class CompareParts
{
public:
bool operator()(const Integer & p1,const Integer & p2)
{
return p1.m
}
};
int main()
{
std::vector vecInteger;
vecInteger.push_back(Integer(12));
vecInteger.push_back(Integer(13));
vecInteger.push_back(Integer(5));
vecInteger.push_back(Integer(7));
vecInteger.push_back(Integer(9));
}
Output:
5 7 9 12 13