Error with sorting an array of structs

This is a discussion on Error with sorting an array of structs within the C++ Programming forums, part of the General Programming Boards category; The following code works when I keep the class outside of main(), but if I define the class in main(), ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    43

    Error with sorting an array of structs

    The following code works when I keep the class outside of main(), but if I define the class in main(), I get the error: error: no matching function for call to ‘sort(main()::student [10], main()::student*)’ Why is this?

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    class student
    {
    public:
        std::string name;
        int grade;
    
        bool operator<(const frequency& rhs) const
        {
            return grade < rhs.grade;
        }
    };
    
    int main()
    {
        student student_array[10];
    
        // snip assignment of names and grades to students here
    
        std::sort(student_array, student_array + 10);
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    hmm... it works? It looks like you mistyped the operator< parameter type to write frequency instead of student.

    Anyway, if you want an explanation of the code that does not work: post it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    Oops. Should have copied and pasted rather than typed it. Here's the broken code as it stands (I made the array smaller for brevity):
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <algorithm>
    
    int main()
    {
        class student
        {
        public:
            std::string name;
            int grade;
        
            bool operator<(const student& rhs) const
            {
                return grade < rhs.grade;
            }
        };
        
        student student_array[4];
    
        student_array[0].name = "Joe";
        student_array[0].grade = 85;
        student_array[1].name = "Sara";
        student_array[1].grade = 72;
        student_array[2].name = "Mike";
        student_array[2].grade = 98;
        student_array[3].name = "Bob";
        student_array[3].grade = 65;
    
        int i;
    
        std::cout << "UNSORTED" << std::endl;
        for(i = 0; i < 4; ++i)
        {
            std::cout << student_array[i].name << ":" << student_array[i].grade << std::endl;
        }
    
        std::sort(student_array, student_array + 4);
    
        std::cout << std::endl << "SORTED" << std::endl;
        for(i = 0; i < 4; ++i)
        {
            std::cout << student_array[i].name << ":" << student_array[i].grade << std::endl;
        }
    
        return 0;
    }
    The compiler issues:
    error: no matching function for call to ‘sort(main()::student [4], main()::student*)’
    If I put the class definition outside main(), if works with no problem, but I don't want to do this if I can avoid it.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    Oh, I see. Frankly, I am not sure why the problem exists and how to resolve it, since I just about never define classes in functions.

    Quote Originally Posted by Mostly Harmless
    If I put the class definition outside main(), if works with no problem, but I don't want to do this if I can avoid it.
    Why do you not want to define your class at namespace instead of function scope? You could define it in an anonymous namespace if necessary.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is that locally defined types cannot be used in template arguments (and sort is a template function).

    The motivation for declaring types locally is stronger with real throw-away types, like very specific predicate functors, but you cannot declare those locally either.

    If you feel your Student class is a throw-away type, not useful for anything else, you could also use std::pair<int, string> (except it will sort students with the same grade in alphabetic order by name - may-be a good thing).
    Last edited by anon; 10-21-2009 at 01:16 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 05:51 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Replies: 12
    Last Post: 12-06-2005, 08:30 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21