我遇到一个问题,我试图初始化一个";阵列";在我的构造函数中,我以前用这个方法做过,它运行得很好,但这次我试图对类进行此操作的是使用继承(不经常使用,所以可能很容易(。我犯了一个错误,说";类"不存在默认构造函数;控制_ Rg_;,但我已经用所有需要的特殊成员函数完全定义了这个类。
下面显示了我认为重要的代码部分。我知道如果我制作了一个构造函数,那么编译器不会为我生成任何构造函数,但我认为Control_Reg_Value_HV类是或确实有默认构造函数。
任何帮助都将是伟大的。提前谢谢。
class Control_Reg_Value_HV;
class Data_SPI;
class Control_Reg_Value_HV{
public:
//----------------Special Member functions-----------------
Control_Reg_Value_HV(uint16_t Control_Write_Value_, uint16_t Control_Store_Value_); //Constructor
Control_Reg_Value_HV(const Control_Reg_Value_HV& C); //Copy Constructor
Control_Reg_Value_HV& operator=(const Control_Reg_Value_HV& C); //Equals Function
~Control_Reg_Value_HV(){} // Destructor
//---------------------------------------------------------
// rest of class, get and sets etc
);
//---------------------------------------------------------
//special member function definitions here
//------------------------Constructor definitions-----------------------------------------
Control_Reg_Value_HV::Control_Reg_Value_HV(uint16_t Control_Write_Value_, uint16_t Control_Store_Value_ )
:Control_Write_Value(Control_Write_Value_),Control_Store_Value(Control_Store_Value_){}
//----------------------------------------------------------------------------------------
//class with inheritance - where the issue is
class Data_SPI:public: Control_Reg_Value_HV{
public:
//----------------Special Member functions-----------------
//static uint16_t position;
Data_SPI()
:Resistance_Val_HV{10,11}{} //how ever many values needed, Issue here
private:
uint16_t Resistance_Val_HV[200];
//---------------------------------------------------------
};
编辑:
no default constructor exists for class "Control_Reg_Value_HV" C/C++(291)[Ln 93, Col 30
对不起,我不知道如何将错误信息以正确的格式放在这里
编辑2:错过了公共:来自继承的类
编辑3:添加了构造函数定义
您的问题是您的Data_SPI
也是Control_Reg_Value_HV
,因此当您尝试构造Data_SPI
时,您必须实例化其成员并构造底层Control_Reg_Value_HV
。
因此,要么向Control_Reg_Value_HV
添加一个默认构造函数,要么显式添加一个具有适当值的构造函数调用:
Data_SPI()
:Control_Reg_Value_HV{ <insert arguments here> },
Resistance_Val_HV{10,11}{}
编辑:**************
Hokay,一个例子:
class Base
{
public:
Base(int x, int y)
: _x{x}, _y{y}
{
}
private:
int _x;
int _y;
};
class Derived : public Base
{
public:
Derived() // Default contructor
: Base{0, 0}, // Call the constructor of Base with appropriate default values
_z{0} // Initialize the members of Derived with appropriate default values
{
}
Derived(int x, int y, int z) // Explicit constructor
: Base{x, y}, // Pass some of the arguments on to the Base constructor
_z{z} // Use the rest to initialize the members of Derived
{
}
private:
int _z;
};