std::list.sort( compFunc ) error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Partridge

    std::list.sort( compFunc ) error

    I have a std list of student objects and I would like to sort them by
    providing a comparison function. I have gotten it to work if my function
    is global, but I would like to encapsulate it in a class. For example, my
    attempt below fails, but if I move sortFunctor out of the class, it will
    compile.

    #include <list>
    using namespace std;

    class foo {
    public:
    foo(int i) : i(i) {};
    ~foo() {};
    int i;

    // compiles if this in the global namespace
    bool sortFunctor(con st foo &f1, const foo &f2) {
    return (f1.i < f2.i);
    }
    };

    int main() {
    list< foo > l;
    foo f1(8);
    foo f2(3);

    l.push_back(f1) ;
    l.push_back(f2) ;

    // compiles if foo:: is removed
    l.sort(foo::sor tFunctor);

    return 0;
    }

    Regards,
    Tim Partridge
  • Karl Heinz Buchegger

    #2
    Re: std::list.sort( compFunc ) error

    Tim Partridge wrote:[color=blue]
    >
    > I have a std list of student objects and I would like to sort them by
    > providing a comparison function. I have gotten it to work if my function
    > is global, but I would like to encapsulate it in a class. For example, my
    > attempt below fails, but if I move sortFunctor out of the class, it will
    > compile.[/color]

    The trick is, that this class needs an operator()
    [color=blue]
    >
    > #include <list>
    > using namespace std;
    >
    > class foo {
    > public:
    > foo(int i) : i(i) {};
    > ~foo() {};
    > int i;
    >
    > // compiles if this in the global namespace
    > bool sortFunctor(con st foo &f1, const foo &f2) {[/color]

    bool operator() ( const foo& f1, const foo& f2 ) {
    [color=blue]
    > return (f1.i < f2.i);
    > }
    > };
    >
    > int main() {
    > list< foo > l;
    > foo f1(8);
    > foo f2(3);
    >
    > l.push_back(f1) ;
    > l.push_back(f2) ;
    >
    > // compiles if foo:: is removed
    > l.sort(foo::sor tFunctor);[/color]

    l.sort( foo() );
    [color=blue]
    >
    > return 0;
    > }
    >[/color]

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Jeff Flinn

      #3
      Re: std::list.sort( compFunc ) error


      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
      news:4011528D.D 7F8B30A@gascad. at...[color=blue]
      > Tim Partridge wrote:[color=green]
      > >
      > > I have a std list of student objects and I would like to sort them by
      > > providing a comparison function. I have gotten it to work if my function
      > > is global, but I would like to encapsulate it in a class. For example,[/color][/color]
      my[color=blue][color=green]
      > > attempt below fails, but if I move sortFunctor out of the class, it will
      > > compile.[/color]
      >
      > The trick is, that this class needs an operator()[/color]

      Why?
      [color=blue]
      >[color=green]
      > >
      > > #include <list>
      > > using namespace std;
      > >
      > > class foo {
      > > public:
      > > foo(int i) : i(i) {};
      > > ~foo() {};
      > > int i;
      > >
      > > // compiles if this in the global namespace
      > > bool sortFunctor(con st foo &f1, const foo &f2) {[/color]
      >
      > bool operator() ( const foo& f1, const foo& f2 ) {
      >[color=green]
      > > return (f1.i < f2.i);
      > > }[/color][/color]

      What state from (*this) is being used here?

      The only thing missing from the OP's class declaration/definition is that
      sortFunctor should be static. I would rename sortFunctor to Compare, as it
      is not a Functor, and it really just compares to foo's.

      Better yet would be to make i private, and provide operator<. That way you
      could just call l.sort().

      Jeff F
      [color=blue][color=green]
      > > };
      > >
      > > int main() {
      > > list< foo > l;
      > > foo f1(8);
      > > foo f2(3);
      > >
      > > l.push_back(f1) ;
      > > l.push_back(f2) ;
      > >
      > > // compiles if foo:: is removed
      > > l.sort(foo::sor tFunctor);[/color]
      >
      > l.sort( foo() );
      >[color=green]
      > >
      > > return 0;
      > > }
      > >[/color]
      >
      > --
      > Karl Heinz Buchegger
      > kbuchegg@gascad .at[/color]


      Comment

      • Karl Heinz Buchegger

        #4
        Re: std::list.sort( compFunc ) error

        Jeff Flinn wrote:[color=blue]
        >
        > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
        > news:4011528D.D 7F8B30A@gascad. at...[color=green]
        > > Tim Partridge wrote:[color=darkred]
        > > >
        > > > I have a std list of student objects and I would like to sort them by
        > > > providing a comparison function. I have gotten it to work if my function
        > > > is global, but I would like to encapsulate it in a class. For example,[/color][/color]
        > my[color=green][color=darkred]
        > > > attempt below fails, but if I move sortFunctor out of the class, it will
        > > > compile.[/color]
        > >
        > > The trick is, that this class needs an operator()[/color]
        >
        > Why?[/color]

        Because the OP wanted to create a Functor. And a functor
        is a class, which has such an operator.
        [color=blue]
        >[color=green]
        > >[color=darkred]
        > > >
        > > > #include <list>
        > > > using namespace std;
        > > >
        > > > class foo {
        > > > public:
        > > > foo(int i) : i(i) {};
        > > > ~foo() {};
        > > > int i;
        > > >
        > > > // compiles if this in the global namespace
        > > > bool sortFunctor(con st foo &f1, const foo &f2) {[/color]
        > >
        > > bool operator() ( const foo& f1, const foo& f2 ) {
        > >[color=darkred]
        > > > return (f1.i < f2.i);
        > > > }[/color][/color]
        >
        > What state from (*this) is being used here?[/color]

        None.
        A Functor is an object that can be used in templates when a function
        is required.
        [color=blue]
        >
        > The only thing missing from the OP's class declaration/definition is that
        > sortFunctor should be static.[/color]

        Thats a bad idea in the general case although it works in this
        specific case.

        --
        Karl Heinz Buchegger
        kbuchegg@gascad .at

        Comment

        • Karl Heinz Buchegger

          #5
          Re: std::list.sort( compFunc ) error

          Jeff Flinn wrote:[color=blue]
          >[/color]

          Dear Jeff. Please disregard my previous post.

          After reareading the OP post, I noticed that I completely
          read a different program then was written down by the OP.

          Somehow I got the expression, that the OP wanted to create
          a functor class named foo.
          Only after rereading I noticed that foo is the class the OP
          builds his list with.


          Sorry for the confusion.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          • Karl Heinz Buchegger

            #6
            Re: std::list.sort( compFunc ) error

            Karl Heinz Buchegger wrote:[color=blue]
            >
            > Jeff Flinn wrote:[color=green]
            > >[/color]
            >
            > Dear Jeff. Please disregard my previous post.
            >
            > After reareading the OP post, I noticed that I completely
            > read a different program then was written down by the OP.
            >
            > Somehow I got the expression, that the OP wanted to create[/color]

            typed to quick: ... got the impression, ...


            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • Jeff Flinn

              #7
              Re: std::list.sort( compFunc ) error


              "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
              news:40117624.A B09FF86@gascad. at...[color=blue]
              > Jeff Flinn wrote:[color=green]
              > >[/color]
              >
              > Dear Jeff. Please disregard my previous post.
              >
              > After reareading the OP post, I noticed that I completely
              > read a different program then was written down by the OP.
              >
              > Somehow I got the expression, that the OP wanted to create
              > a functor class named foo.
              > Only after rereading I noticed that foo is the class the OP
              > builds his list with.[/color]


              Keine Probleme.

              The OP's choice of name "sortFuncto r" didn't help.

              Jeff F


              Comment

              Working...