Reputation: 457
I had a class "SetupModel" with default constructor. Now I have to include one parameter to this constructor. So it should be as,
class SetupModel
{
public:
SetupModel(MyData::ValueSystem& valueSystem);
So I am trying to change all references of this class SetupModel.
But in another one class this has been called as,
class SetupManager
{
private:
SetupModel _model;
am getting error "no appropriate default constructor available"
How can I change this?
Upvotes: 1
Views: 111
Reputation: 41
add a definition of default constructor
class SetupModel
{
public:
SetupModel(MyData::ValueSystem& valueSystem);
SetupModel();
}
Upvotes: 2
Reputation: 15824
You have 2 options in hand
SetupManager
you can define one more constructor within SetupModel
without parametersclass SetupModel { public: SetupModel(MyData::ValueSystem& valueSystem); SetupModel() {};
So you don't need to make change in SetupManager
.
SetupModel
constructor with parameters as base class constructor of SetupManager
, you can use initialization list within constructor of SetupManager
. Then you dont need further change in SetupModel
class SetupManager { SetupManager(...) : _model(valuesystemarg), ... { ... } private: SetupModel _model;
Upvotes: 1
Reputation: 764
You can have as many constructors as you want but if what you want is to have only one then you can set the default value both in .h or in .cpp
in .h:
class SetupManager
{
SetupManager() : _model(value), // here you can set all variables u want separated by a coma
private:
SetupModel _model;
}
in cpp:
SetupManager::SetupManager():
{
_model = value;
}
Be careful so in .h the value is inside "(" and ")" while in cpp you use "=" to assign the value
Upvotes: 0
Reputation: 30489
class SetupManager
{
SetupManager(...) : _model(valuesystemarg), ...
{
...
}
private:
SetupModel _model;
Use initializer list to initialize this member.
Or add the default parameterless constructor to SetupModel
constructor.
Upvotes: 6