“对 `toppers::recordTop' 的未定义引用”错误?

" undefined reference to `toppers::recordTop' " error?

我的问题是: 编写一个包含 class 学生的程序,将学生的详细信息存储在 class 中。从 Student 派生另一个 class Toppers,它仅存储 class

的前 3 名学生的记录

在这里,在继承的 class toppers 中,我获取了派生 class(Toppers) 的数据成员 (recordTop[3]) 并在 top3(toppers a[] 中对其进行了初始化, int n) 函数,但它给我一个未定义引用的错误 `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;
}

如果可以,请指出我的错误?

我刚刚将您的 static topper recordTop[3] 更改为 students recordTop[3]。代码如您所料工作。 class 中 class 成员的实例化以某种方式创建无限递归条件。我添加了几个 \n 以清楚地查看所有语句。

#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;
}