Cause

This error occurs in build stubs for class constructors. When an object of a class is created, all member variables must be initialized. That is, member variables of types that the compiler cannot automatically initialize must be explicitly initialized in the constructor. If the type of the member variable is a class, and that class either doesn’t have a default constructor or is not public, the compiler cannot automatically initialize the member variable. A compilation error can occur if the initialization code for the member variables is incorrectly written in the constructor code.

Solution

This can be solved by referring to the source code and adding the appropriate initialization code for the member variables to the build stub. If the type structure is complex, you need to check all related types and write the appropriate initialization code, as shown in the example below. In the CT 2024.12’s [Stub Troubleshooting Guide] dialog, the location of the code that can be referenced for writing the initialization code is indicated. Referring to this location can help in solving the problem.

Example

Here is an example of the source code and the stub code.

// Source code
class Config {
public:
    Config() = delete;
    Config(int option1, int option2) {}
};

class Processor {
public:
    Processor() = delete;
    Processor(Config config) {}
};

class System {
public:
    System(int kind);
private:
    Processor processor1;
    Processor processor2;
};

void test(System system) {
}
// Stub code
System::System(int kind):
  processor1(Processor()),
  processor2(Processor())
{
/// Stub body start

/// Stub body end
}

By checking the Error View, you can see that the initialization code for the member variables processor1 and processor2 is incorrect.

To correctly initialize the member variable processor1, check the constructor of the Processor class, which is Processor(Config config). Creating an object of the Processor class requires an object of the Config class. To create an object of the Config class, check its constructor, which is Config(int option1, int option2). Therefore, an object of the Config class is created by taking two parameters of type int. Referring to the constructors of both Processor and Config classes, write the initialization code for processor1 as follows:

processor1(Processor(Config(1, 2))) // Enter appropriate values for the parameters according to your test design.

Since the type of processor2 is the same as processor1, initialize processor2 in the same way. Here is the newly written stub code after applying the changes:

// Modified stub code
System::System(int kind):
  processor1(Processor(Config(1, 2))), // Enter appropriate values for the parameters according to your test design.
  processor2(Processor(Config(3, 4)))  // Enter appropriate values for the parameters according to your test design.
{
/// Stub body start

/// Stub body end
}

When you rerun the test or use test compile, you will see that the stub error has been resolved.

Need more help with this?
Don’t hesitate to contact us here.

Thanks for your feedback.