Reputation: 6995
I have many methods the return value of which is a REFERENCE TO in my libraries. When I open the library, I see a C0222 error in the message window for each of these. If I compile the library, the message goes away. The code compiles and works under both CODESYS and TwinCAT.
Is this just a CODESYS glitch, or is there a reason for that message?
Upvotes: 0
Views: 201
Reputation: 21
I can confirm that this happens.
Make a PRG or FB with a method that returns a reference to something, the precompiler throws this error:
C0222: Outputs can't be of type REFERENCE TO
However the code compiles fine, and there are no messages in the normal 'Build' section.
(I think that's why the message 'disappears' in your case. The Messages box switches from Precompile to Build when you compile the library.)
I'd suggest listening to the precompiler and avoid returning a reference.
For example:
METHOD Method1 : REFERENCE TO INT
VAR_INST
i : INT;
END_VAR
Method1 REF= i;
Call like this:
result : REFERENCE TO INT;
result REF= Method1();
This code works fine. When you change result you actually change the i variable inside the method. This feels very hacky!
And when you change i from a VAR_INST to a VAR, the code compiles ok, but the reference now points to invalid memory because i goes out of scope when you leave the method.
Edit:
You can use a pragma to disable the error message. Add the following line in every method where you return a reference:
{warning disable C0222} // Disables Precompiler error C0222: Outputs can't be of type REFERENCE TO
METHOD Method2 : REFERENCE TO BOOL
VAR_INPUT
END_VAR
Upvotes: 0