Cause

A compilation error can occur if appropriate return code for the return type cannot be automatically generated.

Solution

This can be solved by referring to the source code and adding appropriate return code to the build stub. If the hierarchy of the return type is complex, you need to follow the type hierarchy in reverse and appropriately create objects. In 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 OutClass{
 public:
      OutClass() = delete;
      OutClass(int a){}
	  void print(){
	  }
 };

 OutClass& stubError();

 void test(OutClass m){
	 OutClass& b = stubError();
	 b.print();
 }
// Stub code
class OutClass & stubError()
{
/// Stub body start

/// Stub body end
}

By checking the Error View, you can see that the stub function needs to return an object of OutClass.

There are several ways to return an appropriate object:

  • Return a global variable object
  • Declare a static variable inside the stub and return it
  • Return a singleton object

There are various other ways to return an object. You should choose the appropriate method based on the actual source code. In this example, we use the method of declaring a static variable and returning it.

static OutClass obj(1); // Enter appropriate values for the parameters according to your test design.
return obj;

Here is the newly written stub code after applying the changes.

// Modified stub code
class OutClass & stubError()
{
/// Stub body start
	static OutClass obj(1); // Enter appropriate values for the parameters according to your test design.
	return obj;
/// 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.