【问题标题】:" undefined reference to `toppers::recordTop' " error? [duplicate]“未定义对‘toppers::recordTop’的引用”错误? [复制]
【发布时间】:2021-11-12 01:12:59
【问题描述】:

我的问题是: 编写一个程序,该程序有一个 Student 类来存储该类中学生的详细信息。从 Student 派生另一个班级 Toppers,仅存储班级前 3 名学生的记录

在这里,在继承的类 toppers 中,我获取了派生类 (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;
}

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

【问题讨论】:

    标签: c++ oop undefined-reference


    【解决方案1】:

    我刚刚将您的 static topper recordTop[3] 更改为 students recordTop[3]。该代码可以按您预期的方式工作。类内部的类成员的实例化以某种方式创建了类似条件的无限递归。我添加了几个\ns 以清楚地查看所有声明。

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

    【讨论】:

    • 是的,我不知道的无限循环部分
    • 为什么我的问题被关闭了?
    • 我猜stackoverflow已经有类似的问题,因此它会自动关闭。
    猜你喜欢
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多