Absence of Default Constructor in C++ Class

To avoid the so-called cursed diamond problem in virtual inheritance, it is necessary to call a constructor in the initialization list of class D. This results in a single instance of class A being a subobject of D, instead of both classes B and C containing a subobject of class A. However, this approach sacrifices the standard memory layout of classes B, C, and D, even if they don’t have any virtual members. To solve this issue, one can add a default constructor to class A in two ways: by adding it directly to the class definition in Ball.h, or by defining it in Ball.cpp. This solution only works if the default constructor of A has not been deleted by defining any other constructor.


Solution:

The message from the compiler error is unambiguous. It is evident when you employ:

Tokenizer::Tokenizer(const std::string& s, std::ostream& o)
{   //error occurs here
    stream.open(s);
    std::string t;
    getline(stream, t, ' ');
    _token = Token(t);
}

The absence of a default constructor in

std::ostream

results in

os_

being unable to initialize, as the member variable

os_

is default constructed.

It seems that your intention is to set the member variable

os_

to the input parameter of the

Tokenizer

constructor, for example:

Tokenizer::Tokenizer(const std::string& s, std::ostream& o) : os_(o) 
{
   ...

As

std::ostream

does not possess a copy constructor, altering the member variable to a reference object is necessary, as the previous solution will not suffice.

std::ostream& os_;

Then, you can safely use:

Tokenizer::Tokenizer(const std::string& s, std::ostream& o) : os_(o) 
{
   ...

To prevent the

Tokenizer

object from having a dangling reference, it is important to ensure that the

o

input argument is not destroyed prior to the destruction of

Tokenizer

.

Frequently Asked Questions

Posted in Uncategorized