devxlogo

Constructor for a Protected Class

Constructor for a Protected Class

Question:
How do I create a constructor for a protected member of a class? I have declared the function in the header file but not defined the definition.The error message I get from the compiler is below:

make test_MessageCC -c Message.cc "Message.h", line 50: Error: Could not find Message::Message(const char[11], String, int) to initialize base class.1 Error(s) detected.*** Error code 1make: Fatal error: Command failed for target `Message.o'

The line where the error message is pointing to is to an individual message and I believe the error message is saying it can’t initialize the base class message.

Answer:
You can declare a constructor protected only in base classes or classes that should have no instances (for example, abstract classes). A derived class can access all the protected members of its public or protected base class. Therefore, it can invoke the base class’ constructor even if it’s protected. However, a containing class doesn’t have this privilege and is therefore prohibited from accessing a non-public constructor of its member object. Either declare your constructor public, or make it a base class.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist