Main Content

Resolve Error: Class Properties Must Be Fully Defined Before Use

R2026b

Issue

Because C and C++ are statically typed languages, the code generator must be able to determine the types of all variables in the MATLAB® code so that it can define the variables in the generated code. For certain coding patterns, the code generator can be unable to recognize that all class properties have assigned values. These coding patterns include:

  • Assigning values to class properties inside a loop.

  • Copying a handle class property to another object.

  • For an entry-point class, assigning values to class properties outside of the class constructor method.

If the code generator is unable to recognize that you define all of the properties of a MATLAB class, you see an error message containing this sentence:

For code generation, all class properties must be fully defined before use.

Possible Solutions

Depending on the coding pattern that you use, try one of these solutions.

Assign Dummy Values to Class Properties Before Loop

When you assign values to class properties in a loop, the code generator can be unable to recognize that all properties have assigned values. For example, this MATLAB function takes a positive integer input in and returns an instance of class MyClass. The class MyClass has two properties, prop1 and prop2. The function undefinedPropTest assigns values to prop1 and prop2 for the returned myClass object inside a for-loop.

function y = undefinedPropTest(in) %#codegen
arguments
    in (1,1) double {mustBePositive, mustBeInteger}
end
x = MyClass;
for i = 1:in
    x.prop1 = 1 + i;
    x.prop2 = x.prop1 + 3;
end
y = x;
end

In MATLAB execution, undefinedPropTest returns an instance of MyClass for all allowed values of in. However, code generation for undefinedPropTest fails because the code generator is unable to determine that undefinedPropTest assigns values to prop1 and prop2 for all values of in.

To generate code for this function, assign dummy values to all properties of MyClass before the for-loop.

function y = undefinedPropTest(n) %#codegen
arguments
    n (1,1) double {mustBePositive, mustBeInteger}
end
x = MyClass;
x.prop1 = 0;
x.prop2 = 0;
for i = 1:n
    x.prop1 = 1 + i;
    x.prop2 = x.prop1 + 3;
end
y = x;
end

Assign Handle Class Properties Directly

When you copy a handle object property to another object, the code generator can be unable to determine that the MATLAB code defines the copied property. For example, consider the function refHandleTest, which uses the handle class myHandleClass and the value class myValueClass. In this function, three variables reference the same memory location, x, y, and v.prop.

function [out1, out2, out3] = refHandleTest(n) %#codegen
x = myHandleClass;
y = x;
v = myValueClass();
v.prop = x;
x.prop = n;
out1 = x.prop;
out2 = y.prop;
out3 = v.prop.prop;
end

classdef myHandleClass < handle
    properties
        prop
    end
end

classdef myValueClass
    properties
        prop
    end
end

Code generation for refHandleTest fails because the code generator cannot determine that the handle object property v.prop.prop points to the same memory location as x.prop and y.prop. To resolve this error, define v.prop.prop directly.

function [out1, out2, out3] = refHandleTest(n) %#codegen
x = myHandleClass;
y = x;
v = myValueClass();
v.prop = x;
x.prop = n;
v.prop.prop = n;
out1 = x.prop;
out2 = y.prop;
out3 = v.prop.prop;
end

Assign Values to All Properties of Entry-Point Class in Constructor Method

When you generate code for an entry-point class, you must define the types of all class properties in the class constructor method, including all structure fields, cell array elements, and properties of contained classes. Otherwise, the code generator is unable to determine the types of the properties of the entry-point class. For example, code generation for this entry-point class fails:

classdef MyClass
    properties
        p1
        p2
    end

    methods
        function obj = MyClass(in)
            obj.p1 = in;
        end
        function obj = assignNew(obj,in)
            obj.p1 = in;
            obj.p2 = in*10;
        end
        function out = getSum(obj)
            out = obj.p1 + obj.p2;
        end
    end
end

To resolve this error, assign values to all properties in the constructor method.

classdef MyClass
    properties
        p1
        p2
    end

    methods
        function obj = MyClass(in)
            obj.p1 = in;
            obj.p2 = 0;
        end
        function obj = assignNew(obj,in)
            obj.p1 = in;
            obj.p2 = in*10;
        end
        function out = getSum(obj)
            out = obj.p1 + obj.p2;
        end
    end
end

See Also

|

Topics