【问题标题】:Error C3918: Error wile checking event for being nullptr错误 C3918:检查事件是否为 nullptr 时出错
【发布时间】:2011-05-17 17:28:46
【问题描述】:

我的 .h 文件中有委托和事件

delegate void ResponseReceivedHandler(System::Object^ sender, WhoisEventArgs^ e);
event ResponseReceivedHandler^ ResponseReceived; 

在我的代码中某处我想检查事件是否为nullptr,但是当我编写时

已编辑:

if (this->ResponseReceived != nullptr)
{
     this->ResponseReceived(this, e);
}

发生错误

错误 1 ​​错误 C3918:使用需要 'VT::WhoisClient::WhoisRequest::ResponseReceived' 成为数据 成员 c:\Users\ViToBrothers\Desktop\ViTo 项目\VT.WhoisClient\VT.WhoisClient\VT.WhoisRequest.cpp 50

我能做什么?

【问题讨论】:

  • 您编写的代码看起来像 C#,而不是 C++/CLI。
  • 我从 C# 获取示例并想更改为 C++ .NET ...
  • 如果(this.ResponseReceived != null)这一行可能不需要写
  • this. 在 C++ 中不起作用。 this 是一个指针。指针与-> 一起使用。您需要使用正确的 C++ 语法,否则会出现编译错误。
  • 哦抱歉我更正了...

标签: .net events delegates c++-cli


【解决方案1】:

我相信在这种情况下,编译器正在为事件处理程序委托生成访问器函数和后备存储。因此,您不必检查 null。

在此处查看更多信息:http://msdn.microsoft.com/en-us/library/4b612y2s.aspx

【讨论】:

  • 文档here 解释了如何引发事件。
猜你喜欢
  • 2016-01-16
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 2010-11-26
  • 1970-01-01
  • 2019-01-12
相关资源
最近更新 更多