如何在派生 class 上使用初始化列表?
How to use an initializer list on a derived class?
我正在编写一个 Qt
应用程序,其中我有 QDialog
的派生 class。我的派生 class 还使用初始化列表来处理几个私有成员(通过构造函数设置)。现在,即使在允许的语法方面,我也有点困惑。我发现了这个 ,试图在我的小演示脚本中遵循它并得出以下结果:
#include <QString>
#include <QSettings>
#include <QtWidgets/QWidget>
#include <QtWidgets/QDialog>
class foo : QDialog
{
Q_OBJECT
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr) : QDialog{
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent): QDialog(parent){}
};
QString nme_get() {return name;}
void qux(void);
private:
const QString name;
QSettings * const settings;
};
void foo::qux(void) {
QSettings *test = settings;
QString name = nme_get();
test->beginGroup(name);
/*
* ...
*/
test->endGroup();
}
int main (void) {
QWidget *parent = nullptr;
QSettings *sttngs = new QSettings(QSettings::NativeFormat,QSettings::UserScope,"GNU","scaper",nullptr
);
foo inst{sttngs, QString("quux"), parent};
inst.qux();
return 0;
}
编译器抛出:
foo.cpp: In constructor ‘foo::foo(QSettings*, QString, QWidget*)’:
foo.cpp:12:9: error: expected primary-expression before ‘public’
public:
^
foo.cpp:12:9: error: expected ‘}’ before ‘public’
foo.cpp:12:9: error: no matching function for call to ‘QDialog::QDialog(<brace-enclosed initializer list>)’
In file included from /usr/include/x86_64-linux-gnu/qt5/QtWidgets/QDialog:1:0,
from foo.cpp:4:
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:93:5: note: candidate: QDialog::QDialog(QDialogPrivate&, QWidget*, Qt::WindowFlags)
QDialog(QDialogPrivate &, QWidget *parent, Qt::WindowFlags f = 0);
^
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:93:5: note: candidate expects 3 arguments, 1 provided
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:54:14: note: candidate: QDialog::QDialog(QWidget*, Qt::WindowFlags)
explicit QDialog(QWidget *parent = 0, Qt::WindowFlags f = 0);
^
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:54:14: note: conversion of argument 1 would be ill-formed:
foo.cpp:11:9: error: uninitialized const member in ‘class QSettings* const’ [-fpermissive]
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr) : QDialog{
^
foo.cpp:21:28: note: ‘QSettings* const foo::settings’ should be initialized
QSettings * const settings;
^
foo.cpp:12:9: error: expected ‘{’ before ‘public’
public:
我知道错误是由于我没有正确地将类型提交给基础 class 而出现的,但我不确定如何修改它以使其工作...
也在 https://doc.qt.io/qt-5/qdialog.html
它说:QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
编译器说:qdialog.h:93:5: note: candidate: QDialog::QDialog(QDialogPrivate&, QWidget*, Qt::WindowFlags)
- 这让我很困惑,我使用了错误的头文件吗?
您的 class foo 没有继承自 QDialog。将第六行更改为:
class foo : public QDialog {
在定义 c++ classes 时,一般的想法是在头文件(.h 文件)中声明和记录 class 方法,并在 cpp 文件中实现。我目前在没有编辑器的情况下编写这段代码,所以可能会有一些语法错误,但它会让你朝着正确的方向前进:
h文件中:
#include <QString>
#include <QSettings>
#include <QtWidgets/QWidget>
#include <QtWidgets/QDialog>
class foo : public QDialog
{
Q_OBJECT
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr);
QString nme_get();
void qux(); //Removed the void parameter here
private:
const QString name;
QSettings * const settings;
};
在cpp文件中:
// Or whatever your h file is named
#include "foo.h"
foo::foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr)
:QDialog(parent) {
// Do constructor stuff here (like initializing name for instance)
}
void foo::nme_get() {
return name;
}
void foo::qux() {
QSettings *test = settings;
QString name = nme_get();
test->beginGroup(name);
/*
* ...
*/
test->endGroup();
}
我正在编写一个 Qt
应用程序,其中我有 QDialog
的派生 class。我的派生 class 还使用初始化列表来处理几个私有成员(通过构造函数设置)。现在,即使在允许的语法方面,我也有点困惑。我发现了这个
#include <QString>
#include <QSettings>
#include <QtWidgets/QWidget>
#include <QtWidgets/QDialog>
class foo : QDialog
{
Q_OBJECT
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr) : QDialog{
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent): QDialog(parent){}
};
QString nme_get() {return name;}
void qux(void);
private:
const QString name;
QSettings * const settings;
};
void foo::qux(void) {
QSettings *test = settings;
QString name = nme_get();
test->beginGroup(name);
/*
* ...
*/
test->endGroup();
}
int main (void) {
QWidget *parent = nullptr;
QSettings *sttngs = new QSettings(QSettings::NativeFormat,QSettings::UserScope,"GNU","scaper",nullptr
);
foo inst{sttngs, QString("quux"), parent};
inst.qux();
return 0;
}
编译器抛出:
foo.cpp: In constructor ‘foo::foo(QSettings*, QString, QWidget*)’:
foo.cpp:12:9: error: expected primary-expression before ‘public’
public:
^
foo.cpp:12:9: error: expected ‘}’ before ‘public’
foo.cpp:12:9: error: no matching function for call to ‘QDialog::QDialog(<brace-enclosed initializer list>)’
In file included from /usr/include/x86_64-linux-gnu/qt5/QtWidgets/QDialog:1:0,
from foo.cpp:4:
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:93:5: note: candidate: QDialog::QDialog(QDialogPrivate&, QWidget*, Qt::WindowFlags)
QDialog(QDialogPrivate &, QWidget *parent, Qt::WindowFlags f = 0);
^
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:93:5: note: candidate expects 3 arguments, 1 provided
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:54:14: note: candidate: QDialog::QDialog(QWidget*, Qt::WindowFlags)
explicit QDialog(QWidget *parent = 0, Qt::WindowFlags f = 0);
^
/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qdialog.h:54:14: note: conversion of argument 1 would be ill-formed:
foo.cpp:11:9: error: uninitialized const member in ‘class QSettings* const’ [-fpermissive]
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr) : QDialog{
^
foo.cpp:21:28: note: ‘QSettings* const foo::settings’ should be initialized
QSettings * const settings;
^
foo.cpp:12:9: error: expected ‘{’ before ‘public’
public:
我知道错误是由于我没有正确地将类型提交给基础 class 而出现的,但我不确定如何修改它以使其工作...
也在 https://doc.qt.io/qt-5/qdialog.html
它说:QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())
编译器说:qdialog.h:93:5: note: candidate: QDialog::QDialog(QDialogPrivate&, QWidget*, Qt::WindowFlags)
- 这让我很困惑,我使用了错误的头文件吗?
您的 class foo 没有继承自 QDialog。将第六行更改为:
class foo : public QDialog {
在定义 c++ classes 时,一般的想法是在头文件(.h 文件)中声明和记录 class 方法,并在 cpp 文件中实现。我目前在没有编辑器的情况下编写这段代码,所以可能会有一些语法错误,但它会让你朝着正确的方向前进:
h文件中:
#include <QString>
#include <QSettings>
#include <QtWidgets/QWidget>
#include <QtWidgets/QDialog>
class foo : public QDialog
{
Q_OBJECT
public:
foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr);
QString nme_get();
void qux(); //Removed the void parameter here
private:
const QString name;
QSettings * const settings;
};
在cpp文件中:
// Or whatever your h file is named
#include "foo.h"
foo::foo(QSettings* const set=nullptr, const QString s="", QWidget *parent=nullptr)
:QDialog(parent) {
// Do constructor stuff here (like initializing name for instance)
}
void foo::nme_get() {
return name;
}
void foo::qux() {
QSettings *test = settings;
QString name = nme_get();
test->beginGroup(name);
/*
* ...
*/
test->endGroup();
}