Constructor for 'Master' must explicitly initialize the base class 'Hight_student' which does not have a default constructor
时间: 2023-12-22 13:06:33 浏览: 47
这个错误是因为你在派生类(Master)的构造函数中没有显式地调用基类(High_student)的构造函数,而基类又没有默认构造函数。解决方法是在派生类的构造函数中使用成员初始化列表来调用基类的构造函数,例如:
```
class Master : public High_student {
public:
Master(int age, int height, int weight) : High_student(age, height, weight) {
// other constructor code here
}
};
```
在上面的例子中,我们使用成员初始化列表调用了基类的构造函数,以确保派生类对象中的基类子对象能够被正确地初始化。
相关问题
Constructor for 'Model' must explicitly initialize the member 'shader' which does not have a default constructor
The error message you are encountering suggests that there is a member variable called 'shader' in the 'Model' class that does not have a default constructor, and it needs to be explicitly initialized in the constructor of the 'Model' class.
To resolve this issue, you should add an explicit initialization for the 'shader' member variable in the constructor of the 'Model' class. You can do this by using an initializer list.
For example:
```cpp
class Model {
private:
Shader shader; // Assuming 'Shader' is the type of the 'shader' member variable
public:
Model() : shader(/* arguments for shader's constructor */) {
// Constructor code
}
};
```
Replace `Shader` with the actual type of the 'shader' member variable, and provide the necessary arguments for the 'shader' constructor within the initializer list.
By explicitly initializing the 'shader' member variable in the constructor, you ensure that it is properly initialized before any other code in the constructor executes.
constructor for 'MajorityChecker' must explicitly initialize the member 'tree' which does not have a default constructor
这个错误提示是因为在定义类`MajorityChecker`时,它有一个成员变量`tree`,但是这个成员变量没有默认构造函数,也就是说如果不显式地初始化它,就会出现这个错误。
解决这个问题的方法是在`MajorityChecker`的构造函数中显式地初始化`tree`。如果`tree`是一个指针类型,可以使用`new`运算符为它分配内存,并在构造函数中初始化它。例如:
```c++
class MajorityChecker {
public:
MajorityChecker(vector<int>& arr) {
// 为tree分配内存,并将其初始化为NULL
tree = new SegmentTree(0, arr.size()-1, arr);
}
// ...
private:
SegmentTree* tree; // tree是一个指向SegmentTree的指针类型的成员变量
};
```
如果`tree`是一个对象类型,则可以在构造函数的成员初始化列表中初始化它,例如:
```c++
class MajorityChecker {
public:
MajorityChecker(vector<int>& arr) : tree(0, arr.size()-1, arr) {
// ...
}
// ...
private:
SegmentTree tree; // tree是一个SegmentTree对象类型的成员变量
};
```
在上述代码中,`tree`的初始化使用了SegmentTree类的构造函数,并传递了参数0、arr.size()-1、arr,以便初始化tree成员变量。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)