c++ - undefined reference to vtable for DownloadManager -
i trying integrate working code program, receive error displayed on title while compiling. here code:
/**************************************************************************** ** ** copyright (c) 2013 digia plc and/or subsidiary(-ies). ** contact: http://www.qt-project.org/legal ** ** file part of examples of qt toolkit. ** ****************************************************************************/ #include <qcoreapplication> #include <qfile> #include <qfileinfo> #include <qlist> #include <qnetworkaccessmanager> #include <qnetworkrequest> #include <qnetworkreply> #include <qsslerror> #include <qstringlist> #include <qtimer> #include <qurl> #include <stdio.h> qt_begin_namespace class qsslerror; qt_end_namespace qt_use_namespace class downloadmanager: public qobject { q_object qnetworkaccessmanager manager; qlist<qnetworkreply *> currentdownloads; public: downloadmanager(); ~downloadmanager() = 0; void dodownload(const qurl &url); qstring savefilename(const qurl &url); bool savetodisk(const qstring &filename, qiodevice *data); public slots: void execute(); void downloadfinished(qnetworkreply *reply); void sslerrors(const qlist<qsslerror> &errors); }; downloadmanager::downloadmanager() { connect(&manager, signal(finished(qnetworkreply*)), slot(downloadfinished(qnetworkreply*))); } downloadmanager::~downloadmanager() {} void downloadmanager::dodownload(const qurl &url) { qnetworkrequest request(url); qnetworkreply *reply = manager.get(request); #ifndef qt_no_ssl connect(reply, signal(sslerrors(qlist<qsslerror>)), slot(sslerrors(qlist<qsslerror>))); #endif 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)) { // exists, don't overwrite int = 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 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(); // skip first argument, program's name if (args.isempty()) { printf("qt download example - downloads urls in parallel\n" "usage: download url1 [url2... urln]\n" "\n" "downloads urls passed in command-line local directory\n" "if target file exists, .0, .1, .2, etc. appended to\n" "differentiate.\n"); qcoreapplication::instance()->quit(); return; } foreach (qstring arg, args) { qurl url = qurl::fromencoded(arg.tolocal8bit()); dodownload(url); } } void downloadmanager::sslerrors(const qlist<qsslerror> &sslerrors) { #ifndef qt_no_ssl foreach (const qsslerror &error, sslerrors) fprintf(stderr, "ssl error: %s\n", qprintable(error.errorstring())); #else q_unused(sslerrors); #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 (saved %s)\n", url.toencoded().constdata(), qprintable(filename)); } currentdownloads.removeall(reply); reply->deletelater(); if (currentdownloads.isempty()) // downloads finished qcoreapplication::instance()->quit(); } //int main(int argc, char **argv) //{ // qcoreapplication app(argc, argv); // downloadmanager manager; // qtimer::singleshot(0, &manager, slot(execute())); // app.exec(); //} //#include "main.moc"
in code above, removed main()
(i use class) , include "main.moc"
(this gives not found error). added destructor in case.
and here project file in qtcreator:
qt += widgets core network config += console config -= app_bundle sources = main.cpp \ gos.cpp \ download.cpp headers = gos.h qmake_project_name = gos # install target.path = $$[qt_install_examples]/widgets/tutorials/addressbook/part7 installs += target simulator: warning(this example might not work on simulator platform)
what missing?
is code in header file (.h) or in source file (.cpp)? when subclassing qobject, moc depends on class definition being in header.
so split class definition download.cpp download.h , add headers variable. remember make distclean
, call qmake again clean build.
Comments
Post a Comment