【发布时间】:2009-08-19 17:49:47
【问题描述】:
所以,我有一个抽象类Panel 和它的一个实现MyPanel。它们看起来像这样:
class Panel : public QWidget
{
public:
Panel(QWidget* parent = 0) = 0;
virtual ~Panel() = 0;
// but wait, there's more!!
};
class MyPanel : public Panel
{
public:
MyPanel(QWidget* parent = 0);
~MyPanel() {}; // nothing to do here
};
MyPanel::MyPanel(QWidget* parent) :
Panel(parent)
{
// you must construct additional pylons
}
我收到来自 VC++ 的构造函数/析构函数的链接器错误
error LNK2019: unresolved external symbol "public: virtual __thiscall Panel::~Panel(void)" (??1Panel@@UAE@XZ) referenced in function "public: virtual __thiscall MyPanel::~MyPanel(void)" (??1MyPanel@@UAE@XZ) mypanel.obj
error LNK2019: unresolved external symbol "public: __thiscall Panel::Panel(class QWidget *)" (??0Panel@@QAE@PAVQWidget@@@Z) referenced in function "public: __thiscall MyPanel::MyPanel(class QWidget *)" (??0MyPanel@@QAE@PAVQWidget@@@Z) mypanel.obj
为什么会出现此链接器错误?
--- 答案---
class Panel : public QWidget
{
public:
Panel(QWidget* parent = 0) : QWidget(parent) {};
virtual ~Panel() {};
// but wait, there's more!!
};
我以为我在午餐前尝试过这个。原来我错了。
【问题讨论】:
-
您似乎有一些拼写错误。你能澄清一下吗?类声明需要以 ; 结尾例如,类 XXX {};你也有看起来像虚拟构造函数的东西,但这在 C++ 中无效
-
更正了我的拼写错误并添加了正确的解决方案。
-
在您的“答案”中,您应该明确指出您所做的更改(基本上通过 {} 提供空实现)。否则,它留给读者作为练习来比较您的代码示例并了解不同之处。
标签: c++ qt linker abstract-class