1

My C++ class is looking for the default constructor of another object, Throwing error.

I am working on a simple C++ Legacy application. From a class, I am declaring an object of another class in its header file.

In the class constructor, I am trying to call its parameter'ized constructor.

But my program shows the following error as no default constructor.

Derived class Header file

#pragma once
#include "Base.h"
class CDerived :
    public CBase
{
public:
    // CDerived(); // Commented this out
    CDerived(char a);
};

Derived Class Source

#include "Derived.h"
// Only one constructor definition
CDerived::CDerived(char a)
    : CBase(1,2,3)
{
}

My Execute Class header file

#pragma once
#include "Derived.h"
class CExecute
{
public:
    CExecute();
private:
    CDerived d;
};

My Execute class source file

#include "Execute.h"
CExecute::CExecute()
{
    char rslt = SomeFunction();
    d(rslt);
}

Error message

  1. no default constructor exists for class "CDerived"
  2. call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type
  3. 'CDerived': no appropriate default constructor available
  4. term does not evaluate to a function taking 1 arguments

Why is this error message when I am choosing to call my parameter'ized constructor ?

The 'marked duplicate' question is about Initializer list. But, I have already used Initializer list for the derived class. So I am aware of the workings.

I have edited my question to make it more clear. Basically how do I invoke a constructor of my choice for a member variable?

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • To add / emphasize... I am using the initializer list in my Derived class. So the reason why I cant add the initializer list is because I need to get the 'a' at run time. .. i.e. 'a' is the result of another function call – Confused Programmer Apr 10 '19 at 04:48
  • Well when the `{` of the constructor is reached then all data-members not in the initializer list or with a member initialization expression will be default initialized. That's just what the standard says. If `C::C : d(someFunction()){}` is not an option then you'll have to make `d` not a value, but some pointer type instead. Like `std::unique_ptr d;` then you can call `d.reset(new Derived(rslt));` in your constructor. – PeterT Apr 10 '19 at 19:17
  • Ok fair enough. This helped Thank you. – Confused Programmer Apr 11 '19 at 05:55

0 Answers0