除非创建委托实例,否则无法获取函数地址
我有一个定义如下的类:
ref class myClass
{
PictureBox^ pic2;
public:
void setPic2() { pic2 = gcnew PictureBox; }
template<typename UnaryOperator>
void setPic2Click(Form^ x, UnaryOperator op) { pic2->Click += gcnew EventHandler(x, op); }
};
在我的 Windows 窗体类中:
namespace testProject
{
public ref class Form1 : public System::Windows::Forms::Form
{
void Form1_Load(Object^ sender, EventArgs^ e)
{
rect1.setPic2();
rect1.setPic2Click(this, std::bind1st(std::mem_fun(&Form1::pic2_Click), this));
}
void pic2_Click(Object^ sender, EventArgs^ e)
{
// do something...
}
编译时,它生成了与 rect1.setPic2Click 调用相关的错误...:
错误C3374:除非创建委托实例,否则无法获取
'testProject::Form1::pic2_Click'的地址
基本上,我尝试通过创建实例方法setPic2Click<来封装图片框的界面/代码>。这是正确的方法吗?有什么建议如何纠正这个错误吗?
I have a class defined as below:
ref class myClass
{
PictureBox^ pic2;
public:
void setPic2() { pic2 = gcnew PictureBox; }
template<typename UnaryOperator>
void setPic2Click(Form^ x, UnaryOperator op) { pic2->Click += gcnew EventHandler(x, op); }
};
And in my Windows form class:
namespace testProject
{
public ref class Form1 : public System::Windows::Forms::Form
{
void Form1_Load(Object^ sender, EventArgs^ e)
{
rect1.setPic2();
rect1.setPic2Click(this, std::bind1st(std::mem_fun(&Form1::pic2_Click), this));
}
void pic2_Click(Object^ sender, EventArgs^ e)
{
// do something...
}
When compiled, it generated this error which is related to the rect1.setPic2Click
call...:
error C3374: can't take address of
'testProject::Form1::pic2_Click'
unless creating delegate instance
Basically, I tried to encapsulate the interface of the picturebox by create the instance method setPic2Click
. Is this the right approach? Any suggestion how to remedy this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您唯一的错误是您试图以一种不起作用(并且没有意义)的方式混合托管和非托管 C++/CLI 代码。
.NET 委托已经有一个绑定的第一个参数。您所需要的只是:
和
Your only mistake is that you're trying to mix managed and unmanaged C++/CLI code in a way that doesn't work (and doesn't make sense).
.NET delegates already have a bound first parameter. All you need is:
and