-1

Using the code below, I want to use my sort function to take an Student array and sort them based on their gpa component. I have to use the parameters of a Student array and the size of the array. If you look towards the bottom of my int main function I try to call on the member sort to sort the array a but with no avail. The error I get is:

member reference base type Student [200] is not a structure or union.

How do I wrote my code to take array a and say to use the member Sort on it given the parameters I have to use. Thanks in advance. If this too much please let me know I'll try to specify even more.

class Student
{
    private:
        string ID, fname, lname, level;
        double gpa;
    public:
        Student();
        Student(string id, string first, string last, double Gpa, string grade);

        string getID() const;
        string getfirst() const;
        string getlast() const;
        string getlevel() const;
        double getGPA() const;

        void setID(string id);
        void setfirst(string f1);
        void setlast(string l1);
        void setlevel(string lev);
        void setGPA(double GPA1);

        friend void Sort(Student studentlist[], int size);
        friend ostream& operator <<(ostream& ost, Student S1);
};

int main()
{
    ifstream ins;
    ofstream outs;
    ins.open("input.dat");
    outs.open("output.dat");

    if(ins.fail())
    {
        cout << "File not found.";
        exit(1);
    }

    if(outs.fail())
    {
        cout << "Output file not opened.";
        exit(1);
    }


    Student a[200];
    int x = 0;

    while(!ins.eof())
    {
        string id, fnam, lnam, levl;
        double point;
        ins >> id >> fnam >> lnam >> point >> levl;

        a[x].setID(id);
        a[x].setfirst(fnam);
        a[x].setlast(lnam);
        a[x].setGPA(point);
        a[x].setlevel(levl);


        if(a[x].getID() == "")
        {
            break;
        }

        x += 1;
    }

    if(x == 0)
    {
        cout << "File is empty" << endl;
        return 0;
    }

    x = x +1;
    a.Sort(a, x);

    int t=0;
    while(t<x)
    {
        outs << a[t];
        t += 1;
    }


    outs.close();
    ins.close();
    return 0;
}
Dai
  • 110,988
  • 21
  • 188
  • 277
Everlit
  • 27
  • 1

3 Answers3

0

Get rid of the a.. Since Sort is a free function, you need just

Sort(a, x);
aschepler
  • 65,919
  • 8
  • 93
  • 144
0

In C++, arrays are not class objects, so there is no Sort method like there is in C#, however you can use std::sort:

using namespace std;

Student array[200];
// (populate `array` here)

sort(
    begin(array),
    end(array),
    [](const Student& x, const Student& y) -> bool {
        return x.gpa > y.gpa;
    }
);

I recommend using std::Array<T> instead of "raw" arrays for greater runtime safety and to avoid needing to keep track of the array length separately:

I note that you're storing Student objects as values, not pointers, so "moving" a Student to another index in the array will be expensive because it will copy the entire object. Consider allocating Students separately and only sorting an array of Student* pointers instead.

Dai
  • 110,988
  • 21
  • 188
  • 277
0

Use of

a.Sort(a, x);

is incorrect on couple of accounts.

  1. a is an array type, specifically of type Student [200]. Arrays don't have member functions. Hence, use of a. is not allowed.

  2. Sort is a non-member function. Hence it cannot be called with the .Sort() syntax.

Just use:

Sort(a, x);
R Sahu
  • 196,807
  • 13
  • 136
  • 247