Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting a list with class for complex types [modified]

Sorting a list with class for complex types [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
algorithms
31 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Harold_Wishes

    Hello I have designed a program that takes in a list of structs of type data (shown below). The program works fine. But I need a way of sorting the list by decreasing length of string Sequence. In other words, I need to determine the length of each Sequence and position each data so that the longest strings appear first. So I am not comparing strings themselves, but lengths of strings. I know there is a sort function that is part of the <list> class, but I am not sure if I can use it in this situation. Thanks in advance for anyone who can come to a solution. HRW.

    #include <string>
    #include <list>
    #include <iostream>
    #include <fstream>

    using namespace std;

    struct data //
    {
    string Length; //
    string Sequence; //
    string N_Terminal; //
    string C_Terminal;
    };

    list<data> g_DataList;
    list<data>::iterator dataListIter;

    -- modified at 11:29 Monday 10th July, 2006

    F Offline
    F Offline
    FarPointer
    wrote on last edited by
    #4

    you can find it here :- http://www.codeguru.com/forum/showthread.php?t=366063[^] Check this one also :- http://www.codeproject.com/vcpp/stl/functor.asp[^] Regards, FarPointer Blog:FARPOINTER -- modified at 12:49 Monday 10th July, 2006

    H 2 Replies Last reply
    0
    • F FarPointer

      you can find it here :- http://www.codeguru.com/forum/showthread.php?t=366063[^] Check this one also :- http://www.codeproject.com/vcpp/stl/functor.asp[^] Regards, FarPointer Blog:FARPOINTER -- modified at 12:49 Monday 10th July, 2006

      H Offline
      H Offline
      Harold_Wishes
      wrote on last edited by
      #5

      This program for the first link is not compiling and it is hard to see where the sort is taking place. I will check the other link. -- modified at 13:04 Monday 10th July, 2006

      F D 2 Replies Last reply
      0
      • H Harold_Wishes

        This program for the first link is not compiling and it is hard to see where the sort is taking place. I will check the other link. -- modified at 13:04 Monday 10th July, 2006

        F Offline
        F Offline
        FarPointer
        wrote on last edited by
        #6

        Well the other link is for vector you need to customize it, i feel that we need to customize it for list. sort void sort(); Sorts the list elements in ascending order. The comparison operator < ("less than") must be defined for the list element type. Note that the STL sort algorithm does NOT work for lists; that's why a sort member function is supplied. nums.sort(); If we provide the < than operator for the struct its fine i guess. Regards, FarPointer Blog:FARPOINTER -- modified at 13:34 Monday 10th July, 2006

        1 Reply Last reply
        0
        • F FarPointer

          you can find it here :- http://www.codeguru.com/forum/showthread.php?t=366063[^] Check this one also :- http://www.codeproject.com/vcpp/stl/functor.asp[^] Regards, FarPointer Blog:FARPOINTER -- modified at 12:49 Monday 10th July, 2006

          H Offline
          H Offline
          Harold_Wishes
          wrote on last edited by
          #7

          The second link contains a good example. They are using the <vector> class. I am using the <list> class to store my list. I suppose I can use the vector example to implement what I am trying to do with the <list> class. I will have to think about this because I do not think items in lists can be accessed at random like vectors. Perhaps I am wrong on both accounts. :^) Regards again. HRW -- modified at 13:36 Monday 10th July, 2006

          F 1 Reply Last reply
          0
          • H Harold_Wishes

            This program for the first link is not compiling and it is hard to see where the sort is taking place. I will check the other link. -- modified at 13:04 Monday 10th July, 2006

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #8

            Harold_Wishes wrote:

            This program for the first link is not compiling...

            Why not? What compiler error are you receiving?


            "The largest fire starts but with the smallest spark." - David Crow

            "Judge not by the eye but by the heart." - Native American Proverb

            H 1 Reply Last reply
            0
            • H Harold_Wishes

              The second link contains a good example. They are using the <vector> class. I am using the <list> class to store my list. I suppose I can use the vector example to implement what I am trying to do with the <list> class. I will have to think about this because I do not think items in lists can be accessed at random like vectors. Perhaps I am wrong on both accounts. :^) Regards again. HRW -- modified at 13:36 Monday 10th July, 2006

              F Offline
              F Offline
              FarPointer
              wrote on last edited by
              #9

              try overiding the < operator of the struct. Chosing between vector and list you need to be careful bcoz the vector reallocates the whole data on push_back if it doesnt has space to expand ,while list is a linked list .so u need to choose according to your ease. just bcoz you cant do sort on it doesnt mean we have to drop it , you can also write sort from scratch. Regards, FarPointer Blog:FARPOINTER

              1 Reply Last reply
              0
              • D David Crow

                Harold_Wishes wrote:

                This program for the first link is not compiling...

                Why not? What compiler error are you receiving?


                "The largest fire starts but with the smallest spark." - David Crow

                "Judge not by the eye but by the heart." - Native American Proverb

                H Offline
                H Offline
                Harold_Wishes
                wrote on last edited by
                #10

                Well, I hate to place it here, but there were 14 errors. :omg: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2133: 'mylist' : unknown size C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2512: 'list' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2262: 'mylist' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(28) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(30) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2663: 'sort' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2663: 'begin' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2512: 'const_iterator' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : c

                F D 2 Replies Last reply
                0
                • H Harold_Wishes

                  Well, I hate to place it here, but there were 14 errors. :omg: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2133: 'mylist' : unknown size C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2512: 'list' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2262: 'mylist' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(28) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(30) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2663: 'sort' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2663: 'begin' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2512: 'const_iterator' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : c

                  F Offline
                  F Offline
                  FarPointer
                  wrote on last edited by
                  #11

                  Make it list<"MyData"> mylist; Remove the qoutes :) It will work. Regards, FarPointer Blog:FARPOINTER -- modified at 13:54 Monday 10th July, 2006

                  H 1 Reply Last reply
                  0
                  • F FarPointer

                    Make it list<"MyData"> mylist; Remove the qoutes :) It will work. Regards, FarPointer Blog:FARPOINTER -- modified at 13:54 Monday 10th July, 2006

                    H Offline
                    H Offline
                    Harold_Wishes
                    wrote on last edited by
                    #12

                    I still could not get it to run. :confused: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2664: 'void __thiscall std::list >::sort(struct std::greater)' : cannot convert parameter 1 from ' bool (const class MyData &,const class MyData &)' to 'struct std::greater' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2440: 'initializing' : cannot convert from 'class std::list >::iterator' to 'class std::list<_Ty,_A>::const_iter ator' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(37) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list >::iterator' ( or there is no acceptable conversion) C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(39) : error C2228: left of '.m_iData' must have class/struct/union type Error executing cl.exe. Sort.exe - 7 error(s), 0 warning(s)

                    #include <list>
                    #include <string>
                    #include <iostream>
                    #include <algorithm>

                    using namespace std;

                    class MyData
                    {
                    public:
                    int m_iData;
                    string m_strSomeOtherData;
                    };

                    bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs)
                    {
                    return lhs.m_iData < rhs.m_iData;
                    }

                    int main()
                    {
                    // Create list
                    list<MyData> mylist;

                    // Add data to the list
                    MyData data;
                    data.m_iData = 3;
                    mylist.push_back(data);
                    data.m_iData = 1;
                    mylist.push_back(data);

                    // Sort the list using predic

                    F Z 2 Replies Last reply
                    0
                    • H Harold_Wishes

                      Well, I hate to place it here, but there were 14 errors. :omg: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2133: 'mylist' : unknown size C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2512: 'list' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(23) : error C2262: 'mylist' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(28) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(30) : error C2662: 'push_back' : cannot convert 'this' pointer from 'class std::list' to 'class std::list<_Ty,_A> &' Reason: cannot convert from 'class std::list' to 'class std::list<_Ty,_A>' Conversion requires a second user-defined-conversion operator or constructor C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2663: 'sort' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2663: 'begin' : 2 overloads have no legal conversion for 'this' pointer C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2512: 'const_iterator' : no appropriate default constructor available C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : c

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #13

                      Gotta watch those angle brackets:

                      void main( void )
                      {
                      // Create list
                      list<MyData> mylist;
                      ...
                      for (list<MyData>::const_iterator citer = mylist.begin();
                      }


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      F 1 Reply Last reply
                      0
                      • H Harold_Wishes

                        I still could not get it to run. :confused: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2664: 'void __thiscall std::list >::sort(struct std::greater)' : cannot convert parameter 1 from ' bool (const class MyData &,const class MyData &)' to 'struct std::greater' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2440: 'initializing' : cannot convert from 'class std::list >::iterator' to 'class std::list<_Ty,_A>::const_iter ator' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(37) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list >::iterator' ( or there is no acceptable conversion) C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(39) : error C2228: left of '.m_iData' must have class/struct/union type Error executing cl.exe. Sort.exe - 7 error(s), 0 warning(s)

                        #include <list>
                        #include <string>
                        #include <iostream>
                        #include <algorithm>

                        using namespace std;

                        class MyData
                        {
                        public:
                        int m_iData;
                        string m_strSomeOtherData;
                        };

                        bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs)
                        {
                        return lhs.m_iData < rhs.m_iData;
                        }

                        int main()
                        {
                        // Create list
                        list<MyData> mylist;

                        // Add data to the list
                        MyData data;
                        data.m_iData = 3;
                        mylist.push_back(data);
                        data.m_iData = 1;
                        mylist.push_back(data);

                        // Sort the list using predic

                        F Offline
                        F Offline
                        FarPointer
                        wrote on last edited by
                        #14

                        Change it in the for loop also . Regards, FarPointer Blog:FARPOINTER

                        H 1 Reply Last reply
                        0
                        • D David Crow

                          Gotta watch those angle brackets:

                          void main( void )
                          {
                          // Create list
                          list<MyData> mylist;
                          ...
                          for (list<MyData>::const_iterator citer = mylist.begin();
                          }


                          "The largest fire starts but with the smallest spark." - David Crow

                          "Judge not by the eye but by the heart." - Native American Proverb

                          F Offline
                          F Offline
                          FarPointer
                          wrote on last edited by
                          #15

                          how did u bring those mydata in-side the angular brackets . and that horizontal slash in prev post. Regards, FarPointer Blog:FARPOINTER

                          D 1 Reply Last reply
                          0
                          • F FarPointer

                            Change it in the for loop also . Regards, FarPointer Blog:FARPOINTER

                            H Offline
                            H Offline
                            Harold_Wishes
                            wrote on last edited by
                            #16

                            I decided to pick this apart and comment out the for loop because it is still not obvious to me where the problem is. The for loop seems to declare a pointer and initialize it to point to the beginning of the list and simply prints each item on screen until the pointer reaches the end. That aside I notice there appears to be some conversion type error in the line above with mylist.sort(MyDataSortPredicate); See error message below. :sigh: c:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2664: 'void __thiscall std::list >::sort(struct std::greater)' : cannot convert parameter 1 from ' bool (const class MyData &,const class MyData &)' to 'struct std::greater' No constructor could take the source type, or constructor overload resolution was ambiguous Error executing cl.exe. Sort.exe - 1 error(s), 0 warning(s)

                            mylist.sort(MyDataSortPredicate);

                            // Dump the list to check the result
                            /* for (list::const_iterator citer = mylist.begin();
                            citer != mylist.end(); ++citer)
                            {
                            cout << (*citer).m_iData << endl;
                            }*/

                            D 1 Reply Last reply
                            0
                            • F FarPointer

                              how did u bring those mydata in-side the angular brackets . and that horizontal slash in prev post. Regards, FarPointer Blog:FARPOINTER

                              D Offline
                              D Offline
                              David Crow
                              wrote on last edited by
                              #17

                              FarPointer wrote:

                              and that horizontal slash in prev post.

                              Horizontal rule.


                              "The largest fire starts but with the smallest spark." - David Crow

                              "Judge not by the eye but by the heart." - Native American Proverb

                              1 Reply Last reply
                              0
                              • H Harold_Wishes

                                I still could not get it to run. :confused: C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2664: 'void __thiscall std::list >::sort(struct std::greater)' : cannot convert parameter 1 from ' bool (const class MyData &,const class MyData &)' to 'struct std::greater' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2955: 'list' : use of class template requires template argument list c:\program files\microsoft visual studio\vc98\include\list(415) : see declaration of 'list' C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2440: 'initializing' : cannot convert from 'class std::list >::iterator' to 'class std::list<_Ty,_A>::const_iter ator' No constructor could take the source type, or constructor overload resolution was ambiguous C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(36) : error C2262: 'citer' : cannot be destroyed C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(37) : error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list >::iterator' ( or there is no acceptable conversion) C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(39) : error C2228: left of '.m_iData' must have class/struct/union type Error executing cl.exe. Sort.exe - 7 error(s), 0 warning(s)

                                #include <list>
                                #include <string>
                                #include <iostream>
                                #include <algorithm>

                                using namespace std;

                                class MyData
                                {
                                public:
                                int m_iData;
                                string m_strSomeOtherData;
                                };

                                bool MyDataSortPredicate(const MyData& lhs, const MyData& rhs)
                                {
                                return lhs.m_iData < rhs.m_iData;
                                }

                                int main()
                                {
                                // Create list
                                list<MyData> mylist;

                                // Add data to the list
                                MyData data;
                                data.m_iData = 3;
                                mylist.push_back(data);
                                data.m_iData = 1;
                                mylist.push_back(data);

                                // Sort the list using predic

                                Z Offline
                                Z Offline
                                Zac Howland
                                wrote on last edited by
                                #18

                                Harold_Wishes wrote:

                                // Dump the list to check the result for (list::const_iterator citer = mylist.begin(); citer != mylist.end(); ++citer) { cout << (*citer).m_iData << endl; }

                                Don't write your own loop for this.

                                // declared somewhere
                                void printData(const MyData& data)
                                {
                                	cout << data.m_iData << endl;
                                }
                                
                                // use instead of loop
                                for_each(myList.begin(), myList.end(), printData);
                                

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                H 1 Reply Last reply
                                0
                                • Z Zac Howland

                                  Harold_Wishes wrote:

                                  // Dump the list to check the result for (list::const_iterator citer = mylist.begin(); citer != mylist.end(); ++citer) { cout << (*citer).m_iData << endl; }

                                  Don't write your own loop for this.

                                  // declared somewhere
                                  void printData(const MyData& data)
                                  {
                                  	cout << data.m_iData << endl;
                                  }
                                  
                                  // use instead of loop
                                  for_each(myList.begin(), myList.end(), printData);
                                  

                                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                  H Offline
                                  H Offline
                                  Harold_Wishes
                                  wrote on last edited by
                                  #19

                                  This worked. But I still have commented out the code that is suppose to do the sort---> mylist.sort(MyDataSortPredicate);

                                  C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(38) : error C2664: 'void __thiscall std::list<class MyData,class std::allocator<class MyData> >::sort(struct std::greater<class MyData>)' : cannot convert parameter 1 from '
                                  bool (const class MyData &,const class MyData &)' to 'struct std::greater<class MyData>'
                                  No constructor could take the source type, or constructor overload resolution was ambiguous
                                  Error executing cl.exe.

                                  -- modified at 15:03 Monday 10th July, 2006

                                  Z 1 Reply Last reply
                                  0
                                  • H Harold_Wishes

                                    I decided to pick this apart and comment out the for loop because it is still not obvious to me where the problem is. The for loop seems to declare a pointer and initialize it to point to the beginning of the list and simply prints each item on screen until the pointer reaches the end. That aside I notice there appears to be some conversion type error in the line above with mylist.sort(MyDataSortPredicate); See error message below. :sigh: c:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(33) : error C2664: 'void __thiscall std::list >::sort(struct std::greater)' : cannot convert parameter 1 from ' bool (const class MyData &,const class MyData &)' to 'struct std::greater' No constructor could take the source type, or constructor overload resolution was ambiguous Error executing cl.exe. Sort.exe - 1 error(s), 0 warning(s)

                                    mylist.sort(MyDataSortPredicate);

                                    // Dump the list to check the result
                                    /* for (list::const_iterator citer = mylist.begin();
                                    citer != mylist.end(); ++citer)
                                    {
                                    cout << (*citer).m_iData << endl;
                                    }*/

                                    D Offline
                                    D Offline
                                    David Crow
                                    wrote on last edited by
                                    #20

                                    See here for the STL fix.


                                    "The largest fire starts but with the smallest spark." - David Crow

                                    "Judge not by the eye but by the heart." - Native American Proverb

                                    1 Reply Last reply
                                    0
                                    • H Harold_Wishes

                                      This worked. But I still have commented out the code that is suppose to do the sort---> mylist.sort(MyDataSortPredicate);

                                      C:\Documents and Settings\WoodallH\Desktop\C++\Project 6 Nesty tag\Sort.cpp(38) : error C2664: 'void __thiscall std::list<class MyData,class std::allocator<class MyData> >::sort(struct std::greater<class MyData>)' : cannot convert parameter 1 from '
                                      bool (const class MyData &,const class MyData &)' to 'struct std::greater<class MyData>'
                                      No constructor could take the source type, or constructor overload resolution was ambiguous
                                      Error executing cl.exe.

                                      -- modified at 15:03 Monday 10th July, 2006

                                      Z Offline
                                      Z Offline
                                      Zac Howland
                                      wrote on last edited by
                                      #21

                                      You can either use the general sort algorithm (std::sort) or write a specialized less<MyData>() functor that looks something like:

                                      struct std::less
                                      {
                                      	bool operator()(const MyData& lhs, const MyData& rhs)
                                      	{
                                      		return lhs.m_iData < rhs.m_iData;
                                      	}
                                      };
                                      

                                      And then call mylist.sort(std::less<MyData>). If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                      1 Reply Last reply
                                      0
                                      • H Harold_Wishes

                                        Hello I have designed a program that takes in a list of structs of type data (shown below). The program works fine. But I need a way of sorting the list by decreasing length of string Sequence. In other words, I need to determine the length of each Sequence and position each data so that the longest strings appear first. So I am not comparing strings themselves, but lengths of strings. I know there is a sort function that is part of the <list> class, but I am not sure if I can use it in this situation. Thanks in advance for anyone who can come to a solution. HRW.

                                        #include <string>
                                        #include <list>
                                        #include <iostream>
                                        #include <fstream>

                                        using namespace std;

                                        struct data //
                                        {
                                        string Length; //
                                        string Sequence; //
                                        string N_Terminal; //
                                        string C_Terminal;
                                        };

                                        list<data> g_DataList;
                                        list<data>::iterator dataListIter;

                                        -- modified at 11:29 Monday 10th July, 2006

                                        E Offline
                                        E Offline
                                        earl
                                        wrote on last edited by
                                        #22

                                        Harold, You'll do much better if you get (1) a copy of Stroustrup and (2) a copy of Josuttis. Read them in that order. Because what you want to do is trivial and will be explained by the first book; putting in the time for the second will be worth it. earl

                                        H 1 Reply Last reply
                                        0
                                        • E earl

                                          Harold, You'll do much better if you get (1) a copy of Stroustrup and (2) a copy of Josuttis. Read them in that order. Because what you want to do is trivial and will be explained by the first book; putting in the time for the second will be worth it. earl

                                          H Offline
                                          H Offline
                                          Harold_Wishes
                                          wrote on last edited by
                                          #23

                                          Are these C++ authors of C++ books? I will look for them. I did not see anything in the Deitel & Deitel book that was too helpful.

                                          E 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups