简体   繁体   English

QObject多重继承和运算符new

[英]QObject multiple inheritance and operator new

Stuck with this weird question 陷入这个奇怪的问题

Why following code is OK for g++ 为什么以下代码对g ++来说可以

#include <QObject>

class B {
public:
  B(){}
  ~B(){}
};

class A : public QObject, public B {
  Q_OBJECT
public:
  A(QObject * parent = 0 ) : QObject( parent ), B() {}
  ~A(){}
};

int main(int argc, char *argv[])
{
  A a1();
  //A * a = new A();
  //delete a;
  return 0;
}

and this can not be compiled 这无法编译

/*... the same class definitions as above */    

int main(int argc, char *argv[])
{
  //A a1();
  A * a = new A();
  delete a;
  return 0;
}

//error: undefined reference to `vtable for A'

I mean what to do to make the second good as well? 我的意思是该怎么做才能使第二好呢?

PS Well I put everything in separate files, and it works fine. PS好吧,我将所有内容都放在单独的文件中,并且工作正常。 So it is a matter of Q_OBJECT macros, I think. 我想这是Q_OBJECT宏的问题。

Why does the First example compile & Link cleanly while Second doesn't? 为什么第一个示例不能干净地编译并链接而第二个示例却不能干净地编译?

The first example compiles and links because: 第一个示例编译和链接是因为:
It does not create an object of A , 它不会创建A的对象。

A a1();

Declares a function a1() which takes no parameter and returns a A type. 声明一个不带参数且返回A类型的函数a1()

While the Second example creates an object when new is called. 而第二个示例在调用new时创建一个对象。

Note that the *undefined reference to vtable for A'* is a linking error and will only be emitted when a object of class A` is created. 请注意,* vtable for A'* is a linking error and will only be emitted when a object of未定义引用vtable for A'* is a linking error and will only be emitted when a object of创建类A` vtable for A'* is a linking error and will only be emitted when a object of Hence only the Second example shows the error. 因此,只有第二个示例显示了错误。

How to resolve the problem? 如何解决问题?
You need to provide definition for all virtual functions which you derive from QObject . 您需要为从QObject派生的所有虚函数提供定义。

If you define a QObject-derived class, build an application, and realize you forgot to add the Q_OBJECT macro, and you add it later, it is important that you qmake to explicitly update the Makefile. 如果定义了QObject派生的类,构建了一个应用程序,并且意识到您忘记添加Q_OBJECT宏,而后又添加了它,则qmake显式更新Makefile很重要。 Furthermore, to be safe, I recommend a make clean to get rid of old files. 此外,为了安全起见,我建议进行清理以清除旧文件。 make is not smart enough to clean up all of its generated files under such circumstances, and this is an issue that often causes headaches to new Qt developers. 在这种情况下,make不够聪明,无法清理其所有生成的文件,这是一个经常引起新Qt开发人员头痛的问题。

For more information about this error message, see 有关此错误消息的更多信息,请参见

http://cartan.cas.suffolk.edu/oopdocbook/html/commonlinkererrors.html#undefinedreftovtable http://cartan.cas.suffolk.edu/oopdocbook/html/commonlinkererrors.html#undefinedreftovtable

The code works in Vis. 该代码在Vis中有效。 Studio. 工作室。 Your problem may be that B is not a polymorphic class - I don't know why that would give you an error - but you could try making something in B virtual: virtual ~B(){} for example. 您的问题可能是B不是一个多态类-我不知道为什么这会给您带来错误-但您可以尝试使用B虚拟方法制作一些东西:例如virtual ~B(){}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM