【问题标题】:no member function declared in class 'MainWindow'没有在类“MainWindow”中声明的成员函数
【发布时间】:2018-05-20 11:04:53
【问题描述】:

我正在尝试制作一个虚假的 FBI 程序来进行恶作剧,但这并不像我预期的那么容易。 这是我的代码。

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

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

void MainWindow::on_confirmButton_clicked() {
    if (ui->usernameText == "name" && ui->passwordText == "password") {
        ui->resullabel->setText("Password accepted.");
        ui->trackConnButton->setEnabled(true);
    } else {
        ui->resullabel->setText("Password denied.");
    }
}

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

我得到这个错误:

D:\Qt-Projekte\test3\mainwindow.cpp:12: Fehler: no 'void MainWindow::on_confirmButton_clicked()' member function declared in class 'MainWindow'
 void MainWindow::on_confirmButton_clicked() {
                                           ^

我现在的问题是:如何解决? 提前致谢。

我的主窗口.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

【问题讨论】:

  • 询问编译错误时:请复制粘贴错误文本,而不是解释它。
  • 向我们展示MainWindow类的定义(大概在mainwindow.h中)。您似乎忘记了 on_confirmButton_clicked 的函数声明。
  • @0x5453 完成,我希望没问题。
  • @1hund 他们的问题已经回答了您的问题(您是否阅读了整个评论,还是只阅读了第一句话?)。 MainWindow中没有on_confirmButton_clicked的声明。

标签: c++ qt


【解决方案1】:

类的所有成员函数都必须包含“声明”和“定义”。

例如:

struct Foo {
    // Declarations
    void foo() const;
    int bar(int x);
};

// Definitions
void Foo::foo() const {
    std::cout << "foo" << std::endl;
}
int Foo::bar(int x) {
    return x + 1;
}

(注意,如果你在类中内联定义了一个函数,它既是定义又是声明):

struct Foo {
    // Declaration AND definition
    void foo() const {
        std::cout << "foo inline" << std::endl;
    }
};

因此,在您的情况下,您必须在类 MainWindow 的定义中声明 on_confirmButton_clicked 函数。

【讨论】:

  • 我还是不太明白。请用我的代码展示一下好吗?
  • 在MainWindow 类中,在标记为public 的部分中,添加一行void on_confirmButton_clicked();。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多