Cause
This error occurs in build stubs for class constructors. When class Derived
inherits from class Base
, the constructor of Base
is called first when creating an object of Derived
. Even if the constructor of Derived
does not explicitly call the constructor of Base
, the default constructor of Base
is called automatically. If Base
does not have a default constructor, the constructor of Derived
must explicitly call the constructor of Base
. A compilation error can occur if the constructor of the parent class is not explicitly called or an inappropriate constructor is called in the constructor code.
Solution
This can be solved by referring to the source code and adding the appropriate code to call the constructor of the parent class in 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 BaseClass {
public:
BaseClass() = delete;
BaseClass(int t) {}
};
class DerivedClass : public BaseClass {
public:
DerivedClass() = delete;
DerivedClass(BaseClass a) : BaseClass(1) {}
};
class FinalClass : public DerivedClass {
public:
FinalClass(int a);
};
void test(FinalClass a) {
}
// Stub code
FinalClass::FinalClass(int a):
DerivedClass()
{
/// Stub body start
/// Stub body end
}
By checking the Error View, you can see that the initialization code for the parent class DerivedClass
is incorrect.
To correctly initialize the parent class DerivedClass
, check the constructor of the DerivedClass
class, which is DerivedClass(BaseClass a) : BaseClass(1){}
. To initialize the DerivedClass
, an object of the BaseClass
is needed. To create an object of the BaseClass
, check its constructor, which is BaseClass(int t)
. An object of the BaseClass
is created by taking one parameter of type int
. Referring to the constructors of both DerivedClass
and BaseClass
, write the initialization code as follows:
DerivedClass(BaseClass(1)) // Enter appropriate values for the parameters according to your test design.
Here is the newly written stub code after applying the changes:
// Modified stub code
FinalClass::FinalClass(int a):
DerivedClass(BaseClass(1)) // 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.