base class 'class People' should be explicitly initialized in the copy constructor [-Wextra]怎么解决
时间: 2023-06-27 07:03:02 浏览: 51
这个警告是因为在复制构造函数中没有显式地初始化基类 People。可以通过在初始化列表中显示调用基类的复制构造函数来解决这个警告。
示例代码:
```
class People {
public:
People() {}
People(const People& other) {}
};
class Student : public People {
public:
Student() {}
Student(const Student& other) : People(other) {}
};
```
在上面的代码中,Student 继承了 People 类。在 Student 的复制构造函数中,使用初始化列表显式调用了基类 People 的复制构造函数,以确保正确地复制基类的成员变量。这样就可以解决上述警告。
相关问题
Constructor for 'Master' must explicitly initialize the base class 'Hight_student' which does not have a default constructor
这个错误是因为你在派生类(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
}
};
```
在上面的例子中,我们使用成员初始化列表调用了基类的构造函数,以确保派生类对象中的基类子对象能够被正确地初始化。
Qt:constructor for 'dab::DabAsListNvm' must explicitly initialize the base class 'dab::DabListNvm' which does not have a default constructor
这个问题看起来像是你在使用C++的Qt框架时遇到的一个编译错误。
这个错误的原因是,在你的类`DabAsListNvm`的构造函数中,你没有显式地调用`DabListNvm`的构造函数,而`DabListNvm`没有默认构造函数。
为了解决这个问题,你需要在`DabAsListNvm`的构造函数初始化列表中显式地调用`DabListNvm`的构造函数,并传递所需的参数。例如:
```c++
DabAsListNvm::DabAsListNvm(int arg1, double arg2)
: DabListNvm(arg1) // 显式调用基类构造函数
, m_arg2(arg2)
{
// 构造函数的其他代码
}
```
注意,`DabListNvm`的构造函数必须在`DabAsListNvm`的构造函数初始化列表中的第一个调用。