打开项目时的C0222错误(然后消失)

发布于 2025-02-11 05:21:47 字数 124 浏览 1 评论 0原文

我有许多方法的返回值是我的库中的参考。当我打开库时,我会在每个中的消息窗口中看到C0222错误。如果我编译了库,则消息将消失。该代码在CODESYS和TWINCAT下编译和工作。

这只是一个密码的故障,还是该消息的原因?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠意 2025-02-18 05:21:47

我可以确认发生这种情况。
用一种返回对某物的引用的方法制作PRG或FB,预编译器会引发此错误:
C0222:输出不能是类型引用
但是,该代码可以很好地编译,并且在正常的“构建”部分中没有消息。

(我认为这就是为什么消息在您的情况下“消失”的原因。邮件框在编译库时从预编译到构建。)

我建议聆听预编译器并避免返回参考。

例如:

METHOD Method1 : REFERENCE TO INT
VAR_INST
    i : INT;
END_VAR

Method1 REF= i;

这样的调用:

result : REFERENCE TO INT;
result REF= Method1();

此代码正常。当您更改结果时,您实际上更改了方法内的I变量。这感觉很骇人!

而且,当您将i从var_inst更改为var时,代码会编译确定,但是现在的引用指向无效的内存,因为当您离开该方法时,我不超出范围。

编辑:

您可以使用Pragma禁用错误消息。在您返回参考的每种方法中添加以下行:

{warning disable C0222} // Disables Precompiler error C0222: Outputs can't be of type REFERENCE TO
METHOD Method2 : REFERENCE TO BOOL
VAR_INPUT
END_VAR

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文