如何转发在C ++ / CLI中声明一个委托? [英] How do I forward declare a delegate in C++/CLI?

查看:202
本文介绍了如何转发在C ++ / CLI中声明一个委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下内容无效:

 委托MyDelegate; 
ref class MyDelegate;
delegate void MyDelegate;

以下内容用于声明:

  public delegate void MyDelegate(Object ^ sender,MyArgs ^ args); 

但使用它作为转发声明给我

 错误C3756:'MyNameSpace :: MyDelegate':委托定义与现有符号冲突


解决方案

这项工作适用于我;



stdafx.h:

  public delegate void Handler(bool isit); 

cli1.cpp:

  #includestdafx.h
using namespace System;

命名空间MY {
命名空间命名空间
{
public ref class Objeks
{
public:Objeks(){}
public:event Handler ^ OnHandler;
public:void __clrcall Runner(bool checkit)
{
if(& Objeks :: OnHandler!= nullptr)
OnHandler(checkit);
}
};
}
}

我单独离开了默认的VS 2010 C ++ / CLI项目在大多数情况下,我希望如果你经历了前向声明的麻烦,则使用命名空间System;会去标题的还有:)



也许你不想使用事件?但是似乎只是结构。



我在考虑后添加了错误检查(错误编译C ++ / CLI使用Predicate与Array :: FindAll()进行委托调用)。


How?

The following did not work:

delegate MyDelegate;
ref class MyDelegate;
delegate void MyDelegate;

The following works for declaration:

public delegate void MyDelegate(Object ^sender, MyArgs ^args);

But using it as a forward declaration gives me

error C3756: 'MyNameSpace::MyDelegate': delegate definition conflicts with an existing symbol

解决方案

This work's for me;

stdafx.h:

public delegate void Handler(bool isit);

cli1.cpp:

#include "stdafx.h"
using namespace System;

namespace MY {
   namespace Namespace
   {
       public ref class Objeks
       {
           public: Objeks() {}
           public: event Handler^ OnHandler;
           public: void __clrcall Runner(bool checkit)
           {
              if(&Objeks::OnHandler != nullptr) 
                OnHandler(checkit);
           }
       };
   }
}

I left the default VS 2010 C++/CLI project alone for the most part, I would expect that if your going through the trouble of forward declarations, the using namespace System; would go in the header's also :)

Maybe you did not want to use event? But it seems to simply the structure.

I added the error check after considering (Error Compiling C++/CLI Delegate call using Predicate with Array::FindAll()).

这篇关于如何转发在C ++ / CLI中声明一个委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆