qt与Q_OBJECT相关的链接错误

时间:2013-04-17 18:45:48

标签: c++ qt linker qmake qobject

以下是从qt文档站点复制的qt代码示例。

#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QList>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>
#include <QtCore/QStringList>
#include <QtCore/QTimer>
#include <QtCore/QUrl>
#include <QtCore>

#include <stdio.h>

class QSslError;

QT_USE_NAMESPACE

class DownloadManager: public QObject
{
    Q_OBJECT

    QNetworkAccessManager manager;
    QList<QNetworkReply *> currentDownloads;

public:
    DownloadManager();
    void doDownload(const QUrl & url);
    QString saveFileName(const QUrl & url);
    bool saveToDisk(const QString & fileName, QIODevice *data);
    virtual ~DownloadManager();

public slots:
    void execute();
    void downloadFinished(QNetworkReply *reply);
    void sslErrors(const QList<QSslError> & errors);

};


DownloadManager::~DownloadManager()
{

}

DownloadManager::DownloadManager()
{
    connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(downloadFinished(QNetworkReply*)));
}

void DownloadManager::doDownload(const QUrl & url)
{
    QNetworkRequest request(url);
    QNetworkReply* reply = manager.get(request);
    connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>)));
    currentDownloads.append(reply);
}


QString DownloadManager::saveFileName(const QUrl & url)
{
    QString path = url.path();
    QString basename = QFileInfo(path).fileName();

    if ( basename.isEmpty())
        basename = "download";

    if ( QFile::exists(basename))
    {
        int i=0;
        basename += '.';
        while ( QFile::exists(basename + QString::number(i)))
            i++;
        basename += QString::number(i);
    }

    return basename;

}

bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
    QFile file(filename);
    if ( !file.open(QIODevice::WriteOnly))
    {
        fprintf(stderr, "Could not open %s for writing: %s\n",
                qPrintable(filename),
                qPrintable(file.errorString()));
        return false;
    }

    file.write(data->readAll());
    file.close();

    return true;
}


void DownloadManager::execute()
{
    QStringList args = QCoreApplication::instance()->arguments();
    args.takeFirst();

    if ( args.empty() )
    {
        printf("blah blah");
        QCoreApplication::instance()->quit();
        return;
    }

    foreach(QString arg, args)
    {
        QUrl url = QUrl::fromEncoded(arg.toLocal8Bit());
        doDownload(url);
    }
}


void DownloadManager::sslErrors(const QList<QSslError> &errors)
{
//#ifndef QT_NO_OPENSSL
    foreach(const QSslError & error, errors)
        fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
//#endif
}


void DownloadManager::downloadFinished(QNetworkReply *reply)
{
    QUrl url = reply->url();
    if ( reply->error() )
    {
        fprintf(stderr, "Download of %s failed: %s\n",
                url.toEncoded().constData(),
                qPrintable(reply->errorString()));
    }
    else
    {
        QString filename = saveFileName(url);
        if ( saveToDisk(filename, reply))
            printf("Download of %s succeeded 9saved to %s\n",
                   url.toEncoded().constData(), qPrintable(filename));
    }

    currentDownloads.removeAll(reply);
    reply->deleteLater();

    if ( currentDownloads.isEmpty())
        QCoreApplication::instance()->quit();
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    DownloadManager manager;
    //QTimer::singleShot(0, &manager, SLOT(execute()));

    return a.exec();
}

.pro文件是

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-17T11:17:07
#
#-------------------------------------------------

QT       += core network

QT       -= gui

TARGET = network3
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

由于这是从真实网站直接复制的,因此应该正确构建。但它没有建设。

此错误显示..

asit@ubuntu:~/qt/network3-build-desktop$ make
g++ -Wl,-O1 -o network3 main.o    -L/usr/lib/i386-linux-gnu -lQtNetwork -lQtCore -lpthread 
main.o: In function `DownloadManager::DownloadManager()':
main.cpp:(.text+0x584): undefined reference to `vtable for DownloadManager'
main.o: In function `DownloadManager::~DownloadManager()':
main.cpp:(.text+0x61a): undefined reference to `vtable for DownloadManager'
collect2: ld returned 1 exit status
make: *** [network3] Error 1

我按照互联网上的大部分步骤(包括stackoverflow)来摆脱错误。但它不起作用。

有人能在这黑暗中向我展示一些亮点吗?

2 个答案:

答案 0 :(得分:3)

将您的类接口放在.h文件中,问题应该消失!

答案 1 :(得分:3)

您有2个选项。

#1

将此课程保留在main.cpp文件中,但在底部中,您应该添加以下内容:

#include "main.moc"

#2

将类的定义移到头文件中,比如main.h,并且不要忘记将以下内容添加到*.pro文件中:

HEADERS += main.h

注意:您不应将整个班级放入main.h,而只应将其定义为:

class DownloadManager: public QObject
{
    Q_OBJECT
    ...
};

,实施可以保留在main.cpp