C ++中的控制台代码运行期间出现分段错误 [英] Segmentation fault during runtime of console code in C++

查看:101
本文介绍了C ++中的控制台代码运行期间出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的控制台学生详细信息显示系统,其中有2个班级,班级生(基础班级),其成员变量的名称和注册号以及班级StudentAthlete(派生班级),其运动类型字符串。
代码成功编译,但在运行时仅要求提供学生详细信息,但未显示我调用identify()函数时所期望的详细信息。
在代码块上,该错误可能不会显示,但会在在线编译中显示,例如 https:// www.onlinegdb.com/online_c++_compiler ,表明代码中存在分段错误。
请在必要时提供帮助。

I'm trying to make a simple console student details display system whereby there are 2 classes, class student(Base class) with member variables for name and registration number and class studentAthlete(Derived class) with sports type string. The code compiles sucessfully but on runtime only asks for student details but does not display the details as would be expected when I call the identify() function. On code blocks, the error might not show but on online compiles such as https://www.onlinegdb.com/online_c++_compiler it shows that there is a Segmentation fault in the code. Please assist where necessary.

我的代码完全(完整)如下:

My code is exactly(in whole) as is below:

#include <iostream>
using namespace std;

/**Base class**/
class student
{
protected:
    string studName, studRegNum;
public:
    student(string stdName, string regNo);
    /**To define friendly class that is
    Common to both classes**/
    friend void identify();
};
/**constructor for class student**/
student::student(string stdName, string regNo)
{
    studName = stdName;
    studRegNum = regNo;
}
/**Derived class**/
class studentAthlete : public student
{
private:
    string member_sport;
public:
    /**To create constructor for class studentAthlete within class**/
    studentAthlete(string student_sport):student(studName, studRegNum)
    {
        member_sport = student_sport;
    }
    /**To define friendly class that is
    Common to both classes**/
    friend void identify();
};

/**To display student information**/
void identify()
{

    studentAthlete sa(sa.member_sport);
    cout<<"Student Name: "<<sa.studName<<endl;
    cout<<"Student Registration Number: "<<sa.studRegNum<<endl;
    cout<<"Student Sport: "<<sa.member_sport<<endl;

}

int main()
{
    string StudentName, StudentRegistrationNo, StudentSport;
    /**To get & set student name from user**/
    cout<<"Enter student name: "<<endl;
    cin>>StudentName;

    /**To get & set Student Registration No from user**/
    cout<<"Enter Student Registration No: "<<endl;
    cin>>StudentRegistrationNo;

    /**To get & set Student Sport from user**/
    cout<<"Enter Student Sport: "<<endl;
    cin>>StudentSport;

    /**To pass student values to their respective constructors**/
    student st(StudentName,StudentRegistrationNo);
    studentAthlete sa(StudentSport);

    /**To display student information**/
    identify();
    return 0;
}


推荐答案

您正在传递未初始化的成员到 Student 构造函数。

这将导致不确定的行为。

You’re passing uninitialised members to the Student constructor.
This will cause undefined behaviour.

您需要在运动员的构造函数中添加名称和数字参数,并将其传递给 Student

You need to add name and number parameters to the athletes’ constructor and pass those to Student.

这篇关于C ++中的控制台代码运行期间出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆