Visual C++ 2010 错误:LNK2020未解析的令牌

Visual C++ 2010 Error: LNK2020 Unresolved token

提问人:Zomfire 提问时间:12/28/2016 最后编辑:Zomfire 更新时间:12/30/2016 访问量:850

问:

我有以下课程:

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

怎么了?

visual-studio-2010 c++-cli 链接器错误

评论

0赞 Captain Obvlious 12/28/2016
您需要为其提供定义或声明它为纯虚拟。Form1::label1_Click
0赞 Zomfire 12/28/2016
我正在类 Form1 [代码] 虚拟 System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) = 0;[/代码]我有很多错误。我正在使用这个解决方案:[链接]stackoverflow.com/questions/2652198/...
0赞 Zomfire 12/28/2016
1> c:\users\michal\documents\Visual Studio 2010\projects\milionerzy\milionerzy\Form1.h(505):请参阅“Milionerzy::Form1::label1_Click”的声明 1>“void Milionerzy::Form1::label2_Click(System::Object ^,System::EventArgs ^)”:是抽象的

答:

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 类中重写它。