提问人:Zomfire 提问时间:12/28/2016 最后编辑:Zomfire 更新时间:12/30/2016 访问量:850
Visual C++ 2010 错误:LNK2020未解析的令牌
Visual C++ 2010 Error: LNK2020 Unresolved token
问:
我有以下课程:
public ref class Form1 : public System::Windows::Forms::Form
{
//[...]
protected:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e);
};
public ref class Functions : public Form1
{
protected:
void Example() {}
};
public ref class Handlers : public Functions
{
private:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e)
{
Example();
}
};
正如你所看到的,我想把我的方法外表到其他类中。 错误是:
1>Milionerzy.obj:错误 LNK2020:未解析的令牌 (06000004) Milionerzy.Form1::label1_Click
怎么了?
答:
0赞
GeorgeT
12/30/2016
#1
您可能应该从 Form1 中删除label1_Click。处理 label1 click 事件根本没有意义,因为您正在考虑将其设置为纯虚拟事件。只要你有能力就处理它。
如果你想在处理程序中实现多态性,请声明另一个纯虚函数,如下所示:
public ref class Form1 abstract: public System::Windows::Forms::Form
{
//[...]
protected:
virtual void OnLabel1Click()=0;
};
public ref class Functions : public Form1
{
protected:
void Example()
{
}
virtual void OnLabel1Click() override
{
Example();
}
};
public ref class Handlers : public Functions
{
private:
System::Void label1_Click(System::Object^ sender, System::EventArgs^ e)
{
OnLabel1Click();
}
};
评论
0赞
Zomfire
1/2/2017
感谢您的回复。但是在这种情况下,我必须将每个标签(或其他元素)的所有设置放入第一个类中才能在设计中看到它,这必须插入到(public System::Windows::Forms::Form)的子类中。我不想要它。
0赞
GeorgeT
1/2/2017
在这种情况下,删除所有抽象属性并处理 Form1 中的 label1 单击事件,调用虚拟(但不是纯)OnLabelClick。因此,Form1::OnLabelClick 将不执行任何操作,您将在 Handlers 类中重写它。
评论
Form1::label1_Click