Nic
Nic

Reputation: 457

Changing default constructor to parameterized

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

Answers (4)

user5024170
user5024170

Reputation: 41

add a definition of default constructor

class SetupModel
{
public:
   SetupModel(MyData::ValueSystem& valueSystem);
   SetupModel();
}

Upvotes: 2

Steephen
Steephen

Reputation: 15824

You have 2 options in hand

  1. If you prefer to use default constructor without parameters to be used during construction of SetupManager you can define one more constructor within SetupModel without parameters
class SetupModel
{
public:
   SetupModel(MyData::ValueSystem& valueSystem);
   SetupModel() {};

So you don't need to make change in SetupManager.

  1. If you prefer to use 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

Megasa3
Megasa3

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

Mohit Jain
Mohit Jain

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

Related Questions