Home > database >  " undefined reference to `toppers::recordTop' " error? [duplicate]
" undefined reference to `toppers::recordTop' " error? [duplicate]

Time:09-22

My problem was: Write a program that has a class Student to store the details of students in a class. Derive another class Toppers from the Student that stores records of only top 3 students of the class

Here, In the inherited class toppers , I have take taken a data member(recordTop[3]) of derived class(Toppers) and initialised it in top3(toppers a[], int n) function but yet it giving me an error of undefined reference to `toppers::recordTop' .

#include<iostream>
#include<string>
using namespace std;

class students
{
    protected: 
    string name;
    int total;
    public:
        void input_students()
        {
            cout << "Enter your Name : ";
            cin >> name;
            cout << "Enter your total marks : ";
            cin >> total;
        }

        void output_students()
        {
            cout << "Name : " << name << endl;
            cout << "Total Marks : " << total << endl;
        }
};

class toppers : public students
{
    protected:
    static toppers recordTop[3];

    public :
        friend void sort(toppers a[], int n)
        {
            for (int i = 0 ; i < n ; i  )
            {
                for (int j = i ; j < n ;j   )
                {
                    if(a[j].total > a[i].total)
                    {
                        toppers temp = a[j];
                        a[j] = a[i];
                        a[i] = temp;
                    }
                }
            }
        }

        void top3(toppers a[], int n)
        {
            recordTop[0] = a[0];
            recordTop[1] = a[1];
            recordTop[2] = a[2];
        }

        void output_toppers()
        {
            for (int i = 0; i<3 ; i  )
            {
                cout << "Details of RANK " << i   1 << " are : " << endl;
                recordTop[i].output_students();
            } 
        }
};


int main()
{
    int n;
    cout << "Enter number of students : ";
    cin >> n;

    toppers st[n];

    for (int i = 0; i < n ; i  )
    {
        st[i].input_students();
    }

    sort(st,n);

    st[0].top3(st, n);
    st[0].output_toppers(); 
    return 0;
}

If possible , please point my mistake?

CodePudding user response:

I just changed your static topper recordTop[3] to students recordTop[3]. The code works as you might expect. Instantiation of a class member inside the class somehow creates infinite recursion like conditions. I have added a couple of \ns to see all statements clearly.

#include<iostream>
#include<string>
using namespace std;

class students
{
    protected: 
    string name;
    int total;
    public:
        void input_students()
        {
            cout << "\nEnter your Name : ";
            cin >> name;
            cout << "\nEnter your total marks : ";
            cin >> total;
        }

        void output_students()
        {
            cout << "Name : " << name << endl;
            cout << "Total Marks : " << total << endl;
        }
};

class toppers : public students
{
    protected:
    students recordTop[3];

    public :
        friend void sort(toppers a[], int n)
        {
            for (int i = 0 ; i < n ; i  )
            {
                for (int j = i ; j < n ;j   )
                {
                    if(a[j].total > a[i].total)
                    {
                        toppers temp = a[j];
                        a[j] = a[i];
                        a[i] = temp;
                    }
                }
            }
        }

        void top3(toppers a[])
        {
            recordTop[0] = a[0];
            recordTop[1] = a[1];
            recordTop[2] = a[2];
        }

        void output_toppers()
        {
            for (int i = 0; i<3 ; i  )
            {
                cout << "\nDetails of RANK " << i   1 << " are : " << endl;
                recordTop[i].output_students();
            } 
        }
};


int main()
{
    int n;
    cout << "Enter number of students : ";
    cin >> n;

    toppers st[n];

    for (int i = 0; i < n ; i  )
    {
        st[i].input_students();
    }
    
    sort(st,n);
    
    st[0].top3(st);
    st[0].output_toppers(); 
    return 0;
}
  • Related