문제

이 이상한 질문으로 갇혀

왜 다음 코드가 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;
}
.

및 이것은 컴파일 할 수 없습니다

/*... 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'
.

두 번째를 잘 만드는 것에 대해 무엇을 해야할지 모릅니다.

ps 잘 모든 것을 별도의 파일에 넣고 잘 작동합니다.그래서 Q_Object 매크로의 문제, 나는 생각합니다.

도움이 되었습니까?

해결책

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. 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.

For more information about this error message, see

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

다른 팁

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 a1();

Declares a function a1() which takes no parameter and returns a A type.

While the Second example creates an object when new is called.

Note that the *undefined reference to vtable for A'* is a linking error and will only be emitted when a object ofclass A` is created. 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.

The code works in 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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top