LNK Errors in Qt when trying to inherit?

172 Views Asked by At

I'm attempting to call a method from another class Bookmarks, however I keep getting LNK errors when I build my program and I don't understand why.

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: class QStringList     __cdecl Bookmarks::getList(void)" (?getList@Bookmarks@@QEAA?AVQStringList@@XZ) referenced in     function "private: void __cdecl MainWindow::on_save_book_clicked(void)" (?on_save_book_clicked@MainWindow@@AEAAXXZ)

debug\WebBrowser.exe:-1: error: LNK1120: 1 unresolved externals

Of course, the second error exists because of the first one, at least that much I know.

I've declared my headers, including the class I want to use:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "bookmarks.h"

And this is the slot I'm trying to use:

void MainWindow::on_save_book_clicked()
{
    Bookmarks *bm = new Bookmarks();

    QStringList book = bm -> getList();

    QFileDialog *fd = new QFileDialog;
    QString fileName = fd -> getSaveFileName(this,
                                          tr("Save Bookmarks"), "",
                                          tr("Bookmarks (*.txt);;AllFiles (*)"));

    if (fileName.isEmpty())
    {
         return;
    }
    else
    {
         QFile file(fileName);
         if (!file.open(QIODevice::WriteOnly))
         {
             QMessageBox::information(this, tr("Unable to open file"),     file.errorString());
         }
         return;

         QDataStream out(&file);
         out.setVersion(QDataStream::Qt_4_5);
         out << bm;
    }
}    

I've determined that the error is coming from the line:

QStringList book = bm -> getList();

When I comment it out, I no longer get the link error, though I haven't a clue what problem it's causing. Why is this?

2

There are 2 best solutions below

0
On
mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: class QStringList     __cdecl Bookmarks::getList(void)" (?getList@Bookmarks@@QEAA?AVQStringList@@XZ) referenced in     function "private: void __cdecl MainWindow::on_save_book_clicked(void)" (?on_save_book_clicked@MainWindow@@AEAAXXZ)

debug\WebBrowser.exe:-1: error: LNK1120: 1 unresolved externals

This error means that QStringList __cdecl Bookmarks::getList(void) function is not in the library.

It may be possible that you are compiling your code with /Gd option whereas the library is compiled with some other compiler option such as /Gr or /Gv. So:

  1. Compile your code with same compiler option (especially calling convention) as of the library
  2. Or put explicitly calling convention in front of function as

    QString __fastcall(or whatever is required) Bookmarks::getList(void)

This article might help you.

0
On

If it is your own library, and compiler is happy but linker complains, the alternatives are

  • You do not actually have Bookmarks::getLine() method defined (only declared in class definition which makes compiler happy). Note that you may have overloads, so perhaps you forgot this method definition, while having other method definition which is different overload, so be very pedantic when you check this.
  • You forgot to add the library to linking (you only added the include path which makes compiler happy). In qmake, you needs LIBS += -Lpath -lname to add libname library.
  • The library file is actually incompatible with the application object files (compiled with different compiler or options), so linker does not find the symbol there. Solution is to rebuild the library with right compiler and options.

If calling constructor works, but this one method is not found, the first option seems most likely.