-2

When B b(0); is called I am expecting it to call the default constructor to initialize protected data member data_ but it is showing error.

error: no matching function for call to ‘B::B(int)’
candidate expects 0 arguments, 1 provided

Similar is the case for D d(1, 2);.

no matching function for call to ‘D::D(int, int)’
candidate expects 0 arguments, 2 provided
#include <iostream>
using namespace std;

class B
{
protected: // Accessible to child
           // Inaccessible to others
    int data_;

public:
    // ...
    void Print()
    {
        cout << "B Object: ";
        cout << data_ << endl;
    }
};

class D : public B
{
    int info_;

public:
    // ...
    void Print()
    {
        cout << "D Object: ";
        cout << data_ << ", "; // Accessible
        cout << info_ << endl;
    }
};
int main()
{
    B b(0);
    D d(1, 2);
    //b.data_ = 5; // Inaccessible to others

    b.Print();
    d.Print();
    return 0;
}

What should I change to correct this?

273K
  • 29,503
  • 10
  • 41
  • 64
  • 6
    "_I am expecting it to call the default constructor_": A _default constructor_ is by definition a constructor that can be called _with an empty argument list_. Why do you expect `B b(0);` which has a non-empty argument list (`0`) to call a default constructor? – user17732522 Mar 19 '23 at 13:40
  • What do your learning material, books, tutorials or teachers say about constructors and object construction with arguments? – Some programmer dude Mar 19 '23 at 13:42
  • "_What should I change to correct this?_": Define appropriate constructors accepting one and two arguments respectively. – user17732522 Mar 19 '23 at 13:43
  • Hard to be sure since you have snipped possibly relevant code but it seems you misunderstand what a default constructor is. – john Mar 19 '23 at 13:53
  • this might make things clearer about default constructors in C++: [Default parameters with C++ constructors](https://stackoverflow.com/questions/187640/default-parameters-with-c-constructors). And this should also be of use to you [Default constructors](https://en.cppreference.com/w/cpp/language/default_constructor). Good luck. – Abderrahmene Rayene Mihoub Mar 19 '23 at 14:02
  • `// ...` -- Why did you remove the most important parts of the code? Your question has to do with constructors, yet you removed them. It's like saying "something is wrong with my car", but we are not supposed to look at the car. – PaulMcKenzie Mar 19 '23 at 14:04

1 Answers1

1

Default constructor is one that can be called without arguments like B() or D(). Where it defined with default argument B(int data=0) or empty B() where default value of data_ will be default of int type . Constructor you call is user defined (with params B(int) or D(int,int)) while user forgot to define it. Each class responsible for new variable it introduces so constructor of D and B should be:

B(int data) :data_(data) {};
D(int data, int info) :B(data), info_(info) {}; //on data you call constructor of B since B added data_ variable

Your Print() should be virtual in B and must be overridden in D

user10
  • 266
  • 5