Sort function does not work with function object created on stack?

久未见 提交于 2019-12-02 18:34:50

问题


#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.


回答1:


Integer obj2() isn't the definition of an object, it is the declaration of a function named obj2 returning an Integer (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:

struct Foo {};
struct Bar { Bar(Foo); };

Bar quxx(Foo()); // quxx is a function

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:

Bar quxx(Foo (*fn)()); // quxx is the same function as above

To get the definition of a variable initialized with the constructor taking a Foo, you can add a level of parenthesis:

Bar quux((Foo())); // quux is a variable



回答2:


Because obj2 is a function. See this




回答3:


obj2 is not a BinaryPredicate and is invalid as the third parameter to std::sort

obj2 needs to be something like

// Return whether first element is greater than the second
bool UDgreater ( int elem1, int elem2 )
{
   return elem1 > elem2;
}

or the functor type used by obj1.




回答4:


There is no definition of no argument constructor.

Use, Integer obj2(0);

#include<iostream>
#include<vector>
#include<algorithm>
class Integer
{
     public:
     int m;
     Integer(int a):m(a){};
     bool operator()(const Integer p1,const Integer p2)
     {
      return p1.m<p2.m;
     }
};
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(0);
    std::sort(vecInteger.begin(),vecInteger.end(),obj1);
    std::sort(vecInteger.begin(),vecInteger.end(),obj2);

    return 0;
}



回答5:


#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 }
};

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

std::sort(vecInteger.begin(),vecInteger.end(),CompareParts()); 
typedef vector<Integer>::const_iterator Iter;
Iter beg = vecInteger.begin();
Iter end = vecInteger.end();

for (Iter iter = beg; iter != end; ++iter)
    cout << (*iter).m << " ";

cout << endl;

}

Output: 5 7 9 12 13



来源:https://stackoverflow.com/questions/1300327/sort-function-does-not-work-with-function-object-created-on-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!