我在Windows 7 Ultimate 32位上使用Qt Creator 2.0.1和Qt 4.7.0(32位).
请考虑以下代码,这是产生错误的最小代码:
class T : public QObject, public QGraphicsItem
{
Q_OBJECT
public:
T() {}
QRectF boundingRect() const {return QRectF();}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget) {}
};
int main()
{
T t;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段导致以下链接器错误:
在函数"T"中:
未定义引用`vtable for T'
未定义引用`vtable for T'
在函数`~T'中:
未定义引用`vtable for T'
未定义引用`vtable for T'
如果我注释掉包含的行Q_OBJECT,它编译得很好.我需要信号和插槽,QGraphicsItem所以我需要Q_OBJECT.
代码有什么问题?谢谢.
这是我的标题:
#ifndef BARELYSOCKET_H
#define BARELYSOCKET_H
#include <QObject>
//! The First Draw of the BarelySocket!
class BarelySocket: public QObject
{
Q_OBJECT
public:
BarelySocket();
public slots:
void sendMessage(Message aMessage);
signals:
void reciveMessage(Message aMessage);
private:
// QVector<Message> reciveMessages;
};
#endif // BARELYSOCKET_H
Run Code Online (Sandbox Code Playgroud)
这是我的班级:
#include <QTGui>
#include <QObject>
#include "type.h"
#include "client.h"
#include "server.h"
#include "barelysocket.h"
BarelySocket::BarelySocket()
{
//this->reciveMessages.clear();
qDebug("BarelySocket::BarelySocket()");
}
void BarelySocket::sendMessage(Message aMessage)
{
}
void BarelySocket::reciveMessage(Message aMessage)
{
}
Run Code Online (Sandbox Code Playgroud)
我收到链接器错误:
undefined reference to 'vtable for BarelySocket'
Run Code Online (Sandbox Code Playgroud)
Message是一个复杂的struct …以下链接示例:http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html
#include <QObject>
#include <QPushButton>
#include <iostream>
using namespace std;
class MyWindow : public QWidget
{
Q_OBJECT // Enable slots and signals
public:
MyWindow();
private slots:
void slotButton1();
void slotButton2();
void slotButtons();
private:
QPushButton *button1;
QPushButton *button2;
};
MyWindow :: MyWindow() : QWidget()
{
// Create button1 and connect button1->clicked() to this->slotButton1()
button1 = new QPushButton("Button1", this);
button1->setGeometry(10,10,100,40);
button1->show();
connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));
// Create button2 and connect button2->clicked() to this->slotButton2()
button2 = new QPushButton("Button2", this);
button2->setGeometry(110,10,100,40); …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个基本上像indianwebproxy一样工作的Http代理
所以我启动了qtcreator,但是我的一个类无法使用臭名昭着的错误进行编译:undefined reference to vtable for HttpProxyThreadBrowser.我无法弄清楚为什么这样做.我在Stackoverflow上阅读了类似的问题,显然问题是未定义的虚拟方法不纯,但我还没有声明任何虚函数.这是我的课
class HttpProxyThreadBrowser : public QThread
{
public:
HttpProxyThreadBrowser(QTcpSocket outgoingSocket,QTcpSocket browserSocket,QObject *parent = 0);
~HttpProxyThreadBrowser(){};
void run();
private:
QTcpSocket outgoingSocket;
QTcpSocket browserSocket;
};
Run Code Online (Sandbox Code Playgroud)
我在这里用pastebin定义了这个类,以免给你带来烦恼.不幸的是,我无法找出为什么vtable未定义.请协助.
httpproxythreadbrowser.cpp:5: undefined reference to `vtable for HttpProxyThreadBrowser
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)