【问题标题】:How do I forward declare a delegate in C++/CLI?如何在 C++/CLI 中转发声明委托?
【发布时间】:2009-06-17 08:50:46
【问题描述】:

怎么做?

以下操作无效:

delegate MyDelegate;
ref class MyDelegate;
delegate void MyDelegate;

声明的工作如下:

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

但是使用它作为前向声明给了我

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

【问题讨论】:

  • 一个建议是转发声明 MyArgs,但即使这样也无济于事。有人可以帮忙吗?

标签: .net syntax c++-cli delegates forward-declaration


【解决方案1】:

这项工作是给我的;

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);
           }
       };
   }
}

我大部分时间都保留了默认的 VS 2010 C++/CLI 项目,我希望如果您遇到前向声明的麻烦,使用命名空间系统;也会进入标题的:)

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

我在考虑后添加了错误检查(Error Compiling C++/CLI Delegate call using Predicate with Array::FindAll())。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多