c++ - 看到对 `vtable for CollidingMice' 的 undefined reference

标签 c++ qt vtable

我正在修改 Qt 代码附带的 Qt 示例“collidingmice”。
在原始源代码中,QApplication 包含 QView 和 QScene,但我制作了一个包含 QView 和 QScene 的类 CollidingMice 以使用键盘输入来终止 View 和场景。我想将键盘输入发送到 CollidingMice 类。 我在堆栈溢出中阅读了 4 或 5 个关于“未定义对 vtable 的引用...”的问题,但找不到适合我的情况。我查过了
1.父类中没有未定义的虚函数
2. 我尝试添加析构函数的定义 ~CollidingMices() {}
3. 我 99% 确定下面的 CollidingMice 代码中没有未定义的成员函数。

#include "mouse.h"

#include <QtGui>


#include <math.h>

static const int MouseCount = 7;
class CollidingMice : public QMainWindow
{
        Q_OBJECT

        private:
                QGraphicsView *view;
                QGraphicsScene scene;
                QTimer *timer;

        public:
                CollidingMice(QWidget *parent = 0): QMainWindow(parent) {
                        scene.setSceneRect(-300, -300, 600, 600);
                        scene.setItemIndexMethod(QGraphicsScene::NoIndex);
                        for (int i = 0; i < MouseCount; ++i) {
                                Mouse *mouse = new Mouse;
                                mouse->setPos(::sin((i * 6.28) / MouseCount) * 200,
                                                ::cos((i * 6.28) / MouseCount) * 200);
                                scene.addItem(mouse);
                        }
                        view = new QGraphicsView(this);
                        view->setRenderHint(QPainter::Antialiasing);
                        view->setBackgroundBrush(QPixmap(":/images/cheese.jpg"));
                        view->setCacheMode(QGraphicsView::CacheBackground);
                        view->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
                        view->setDragMode(QGraphicsView::ScrollHandDrag);
                        view->setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Colliding Mice"));
#if defined(Q_WS_S60) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
                        view->showMaximized();
#else
                        view->resize(600, 450);
                        view->move(30,30);
                        view->show();
#endif
                        timer = new QTimer;
                        QObject::connect(timer, SIGNAL(timeout()), &scene, SLOT(advance()));
                        timer->start(1000 / 33);
                }

        private:
                void keyPressEvent(QKeyEvent *event);
};

void  CollidingMice::keyPressEvent(QKeyEvent *event)
{
                if (event->key() == Qt::Key_q) {
                                close();
                }
}

int collidingmice_main(int argc, char **argv)
{
                QApplication app(argc, argv);
                qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));

                CollidingMice w;

                return app.exec();
}

添加和编辑:按照 svlasov 告诉我的那样删除上面的 QOBJECT 之后,并按如下方式修复构造函数之后(请参阅 setScene.. 我的同事建议我。)

               view = new QGraphicsView(this);
                view->resize(600,500);
                view->setScene(&scene);
                view->setRenderHint(QPainter::Antialiasing);

我可以编译并执行它。

enter image description here

最佳答案

如果您在类定义中使用Q_OBJECT,您必须将类提取到单独的头文件中。

如果您没有在 CollidingMice 类中声明信号和槽,只需删除 Q_OBJECT 即可编译。

更新

正如@KubaOber 评论的那样,您可以简单地在 file.cpp 文件的末尾包含:

#include "file.moc"

qmake 将完成所有工作。

关于c++ - 看到对 `vtable for CollidingMice' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29257380/

相关文章:

c++ - 重新声明为另一种符号错误(c++)

c++ - 我可以在 C++ 程序之外使用随机内存地址访问随机数据吗

c++ - QML 如何捕捉组件转换变化

c++11 - 据推测,链接器指的是对 vtable 的 undefined reference

c++ - STL Vector比较问题

c++ - 为什么这个没有参数的构造函数似乎是这段代码的问题?

c++ - Qt:QSplitter,重做大小

如果通过 cronjob 执行应用程序,Qt 输出(qDebug qWarning 等)不起作用

c++ - 在 COM 接口(interface)的 Vtable 中查找特定函数的索引

c++ - 利用 Visual Studio 调试器中看到的虚拟指针表地址