C++ inheritance getting error

前端 未结 2 1383
不思量自难忘°
不思量自难忘° 2021-01-21 13:12

I\'m having a bit of trouble figuring out this error I have. So I have a Person class, and a Student subclass.

The Person class has the following constructor:



        
2条回答
  •  情歌与酒
    2021-01-21 13:47

    You are attempting to call Person's default constructor, but it doesn't have one. In any case, it looks like what you want to do is call its single parameter constructor:

    Student::Student(const string& name, int regNo) : Person(name), regNo(regNo)
    {
    }
    

    Similarly, use the constructor initialization list in Person's consructor:

    Person(const string& name) : name(name) {}
    

    This way, you are initializing your data members to the desired value, instead of default initializing them and then assigning them new values.

提交回复
热议问题