qt初级错误:头文件中定义,cpp未实现

错误信息

问题

:-1: error: symbol(s) not found for architecture x86_64
:-1: error: linker command failed with exit code 1 (use -v to see invocation)

编译输出

Undefined symbols for architecture x86_64:
“MainWindow::slotQuestion()”, referenced from:
MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) in moc_mainwindow.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: * [Hello2.app/Contents/MacOS/Hello2] Error 1
22:39:31: 进程”/usr/bin/make”退出,退出代码 2 。
Error while building/deploying project Hello2 (kit: Desktop Qt 5.5.1 clang 64bit)
When executing step “Make”

原因

引起原因是头文件中定义的slot,在cpp中没有实现,导致编译错误

代码

错误代码

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
private slots:
    void slotQuestion();
};

#endif // MAINWINDOW_H

mainwindow.cpp

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

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

    setWindowTitle(tr("HelloQt"));
    connect(btn1,SIGNAL(clicked(bool)),this,SLOT(slotQuestion()));

}

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

修改

只需在mainwindow.cpp中加上实现slot就ok了,
void MainWindow::slotQuestion()
{
}

你可能感兴趣的:(QT)