如何在C ++ / CLI中转发声明委托?

时间:2021-04-06 20:53:34

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

1 个解决方案

#1


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 :)

我大部分都是单独离开了默认的VS 2010 C ++ / CLI项目,我希望如果你经历了前向声明的麻烦,那就使用命名空间系统;会进入标题也:)

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()).

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

#1


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 :)

我大部分都是单独离开了默认的VS 2010 C ++ / CLI项目,我希望如果你经历了前向声明的麻烦,那就使用命名空间系统;会进入标题也:)

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()).

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