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


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

MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    delete ui;

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


    namespace Ui {
class MainWindow;

class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

public slots:
    void buttonClickHandler();

public slots:
    void on_openChrome_clicked();

    Ui::MainWindow *ui;

What am I doing wrong?


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:


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


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).


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.