Thread: Error with sorting an array of structs

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 12: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, 04:51 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07: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, 09:54 AM
  5. sorting an array of structs
    By singletrackmind in forum C++ Programming
    Replies: 8
    Last Post: 11-13-2001, 03:33 AM