我在一本书中使用了一个例子,“C ++ Programming a gui with qt4 second edition”。虽然我目前正在运行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();
然而,这给我一个非法的间接错误。
答案 0 :(得分:4)
dialog
不是指针,因此您无法使用->
运算符。
findDialog dialog = new findDialog;
应该是
findDialog * dialog = new findDialog;
^^^ pointer
答案 1 :(得分:1)
此行不正确
findDialog dialog = new findDialog;
您正在使用会返回指针的new
,它应该是
findDialog* dialog = new findDialog;