c++ - Qt "no matching function for call"

标签 c++ qt compiler-errors

我有

no matching function for call to 'saveLine::saveLine()'

编译我的应用程序时出错。从未实际调用构造函数。

saveLine类定义:

class saveLine
{
public:
    saveLine(QWidget *parent);
private:
    QPushButton *selectButton, *acceptButton;
    QLabel *filePath;
    QLineEdit *allias;
};

saveLine 在另一个类中使用,定义如下:

class MWindow : public QWidget
{
    Q_OBJECT
public:
    MWindow(QWidget *parent=0);
private:
    saveLine line1;
};

错误指向 MWindow 构造函数实现

MWindow::MWindow(QWidget *parent):QWidget(parent)
{
    this->setWindowTitle("Launcher");
    this->resize(600,600);
}

我该怎么办?我打算在 vector 中使用 saveLine 类,在运行时创建线。

编辑:我误报了第 1 行,它应该是

saveLine *line1;

但是现在又报错了

ISO C++ forbids declaration of 'saveLine' with no type

expected ';' before '*' token

在这条线上。 saveLine 似乎不再被视为一个类,这是怎么回事?

最佳答案

由于您为类 saveLine 提供了用户声明的构造函数,因此编译器不提供默认构造函数。您的构造函数不是默认构造函数(它有一个必需的参数),因此您不能默认构造 saveLine 类型的对象。

因为你的 MWindow 类中有一个 saveLine 对象,你需要使用你的构造函数初始化它,在 MWindow 构造函数的初始化列表中:

MWindow::MWindow(QWidget *parent)
    : QWidget(parent), line1(parent)
{
    //...
}

(我假设父指针是你要传递的指针;如果你需要给它别的东西,那就给它它需要的东西)

另一种选择是为 saveLine 的构造函数中的参数提供默认参数:

saveLine(QWidget *parent = 0);

这将允许在不带参数的情况下调用该构造函数(并使其成为默认构造函数)。这样做是否有意义取决于父指针是否真的是可选的。显然,如果您这样做,在取消引用和使用它之前需要检查以确保指针不为空。

关于c++ - Qt "no matching function for call",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2676503/

相关文章:

C++ : Pushing an object into a vector

c++ - CMFCToolBarComboBoxButton 中的 AddItem - 未显示新值

c++ - 我如何确定 Qt 中的对话框是否为空?

c++ - 更改 QProgressBar 中的填充

c# - 无法加载类型 'newVersion_Default'

java - 查找给定输入的数据类型

C++解析文件路径

python - 在 QTabWidget 选项卡下创建一个 QMenubar

java - Java Gxt 中 ListStore 的迭代

java - XUGGLE 错误 : Exception in thread "main" java. lang.NoClassDefFoundError: org/slf4j/LoggerFactory