Undefined reference in simple Qt program

I'm a beginner making a program in Qt creator. I made a button that should open Google Chrome using QtProcess::execute(), but I'm getting following errors:

F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\build-QtMainLProject-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\moc_mainwindow.cpp:71: error: undefined reference to `MainWindow::buttonClickHandler()'`
:-1: error:  ld returned 1 exit status

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_openChrome_clicked()
{
    QString exeloc = "F:\\Users\\Amol-2\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
    QProcess::execute(exeloc);
}

mainwindow.h:

    namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void buttonClickHandler();


public slots:
    void on_openChrome_clicked();

private:
    Ui::MainWindow *ui;
};

What am I doing wrong?

Answers


You have this in your .h file:

public slots:
    void buttonClickHandler();

It's a method declaration, and it's a Qt slot, so Qt moc will generate code which references it (to call it for connected signals etc). And then linker tries to link that code to create your application binary. But you don't have the method defintion (actual code) anywhere, it seems.

3 possible fixes:

1.

Remove that slot declaration from the .h file, since you don't seem to be using it.

2.

Add defionition by changing above snippet to this in the .h file:

public slots:
    void buttonClickHandler() { /* add code if you want some */ }

This turns the declaration into a definition (of inline member function).

3.

Alternatively, add method definition to the .cpp file, like you have for your other methods:

void MainWindow::buttonClickHandler() {
    // your code here
}

Need Your Help

Navigation makes both divs to scroll

javascript jquery css slider

I am using Owl Graphics sliders on my webpage.

iOS - Property 'name' not found on object of type 'NSArray'

ios iphone objective-c ios7

My question is similar to this one but I feel different enough to warrant me asking here.