没有重载运算符 ->
no overloaded operator ->
我使用了一本书中的一个例子,"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;
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 是否需要使用 - &gt;运算符在C 中调用成员函数时