P4045¶
- Code
P4045
- Message
Function block is ABSTRACT and cannot be instantiated
This error occurs when a variable is declared with the type of a
function block marked ABSTRACT (the CODESYS/TwinCAT vendor
extension). An ABSTRACT function block exists only to be extended
via EXTENDS – it cannot be instantiated directly.
Example¶
The following code will generate error P4045:
FUNCTION_BLOCK ABSTRACT FB_Base
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB_User
VAR
inst : FB_Base;
END_VAR
END_FUNCTION_BLOCK
To fix this error, declare the variable with a concrete (non-abstract) function block type that extends the abstract base instead:
FUNCTION_BLOCK ABSTRACT FB_Base
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB_Concrete EXTENDS FB_Base
END_FUNCTION_BLOCK
FUNCTION_BLOCK FB_User
VAR
inst : FB_Concrete;
END_VAR
END_FUNCTION_BLOCK
Extending an abstract function block via EXTENDS is unaffected by
this error – only directly declaring a variable of the abstract type
itself is rejected.
Verified against a real TcXaeShell compile (C0434: Function block
FB_Base is ABSTRACT and cannot be instantiated) before this check was
implemented. Covered by rule_abstract_not_instantiated::tests in the
compiler’s own test suite.
Think IronPLC is wrong about this?
If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.