没有重载运算符 ->

no overloaded operator ->

本文关键字:gt 运算符 重载      更新时间:2023-10-16

我使用了一本书中的一个例子,"c++编程gui with qt4第二版"。虽然我目前正在运行qt5,但大多数示例只需要从书中进行微小的更改。

然而,这个特殊的例子表明,我需要重载'->'操作符,因为我的一个操作数是类类型的。虽然我在Visual c++中对类使用了这种语法,但它从来没有抱怨过它没有被重载。

main.cpp

第11行出现错误

代码如下:

findDialog.h

#ifndef FINDIALOG
#define FINDIALOG
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class findDialog : public QDialog  
{
    Q_OBJECT
public:
    findDialog(QWidget *parent = 0);

signals:
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrevious(const QString &str, Qt::CaseSensitivity cs);
private slots:
    void findClicked();
    void enableFindButton(const QString &text);
private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif // FINDIALOG

这是findDialog.cpp

#include <QtWidgets>
#include "findialog.h"
findDialog::findDialog(QWidget *parent) : QDialog(parent)
{
    label = new QLabel(tr("Find &what"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);
    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox (tr("search &backward"));
    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton = new QPushButton("&Close");
    connect(lineEdit, SIGNAL(textChanged(const QString &)), this
    SLOT(enableFindButton(const QString &)));                                 
    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
    QHBoxLayout *topLeftBox = new QHBoxLayout;
    topLeftBox->addWidget(label);
    topLeftBox->addWidget(lineEdit);
    QVBoxLayout *leftBox = new QVBoxLayout;
    leftBox->addLayout(topLeftBox);
    leftBox->addWidget(caseCheckBox);
    leftBox->addWidget(backwardCheckBox);
    QVBoxLayout *rightBox = new QVBoxLayout;
    rightBox->addWidget(findButton);
    rightBox->addWidget(closeButton);
    rightBox->addStretch();
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftBox);
    mainLayout->addLayout(rightBox);
    setLayout(mainLayout);
    setWindowTitle(tr("Find"));
    setFixedHeight(sizeHint().height());
}
void findDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ?     
    Qt::CaseSensitive : Qt::CaseInsensitive;
    if(backwardCheckBox -> isChecked())
    {
        emit findPrevious(text, cs);
    }
    else
    {
        emit findNext(text, cs);
    }
}
void findDialog::enableFindButton(const QString &text)
{
    findButton->setEnabled(!text.isEmpty());
}

最后main.cpp(第11行出现错误)

#include "mainwindow.h"
#include <QApplication>
#include "findialog.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    findDialog dialog = new findDialog;
    //Error occurs on following line
    dialog->show();
    return a.exec();
}

我也尝试了原始/丑陋的语法:

(*dialog).show();

dialog不是指针,所以不能使用->操作符。

findDialog dialog = new findDialog;
应该

findDialog *  dialog = new findDialog;
           ^^^ pointer

这一行不正确

findDialog dialog = new findDialog;

你正在使用new,它将返回一个指针,它应该是

findDialog* dialog = new findDialog;