gpt4 book ai didi

c++ - 带有参数的自定义对象的 QVector?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:21:59 27 4
gpt4 key购买 nike

我正在尝试将 QVector 与名为 RoutineItem 的自定义对象一起使用。

但是报错:

C:\Qt\5.2.1\mingw48_32\include\QtCore\qvector.h:265: error: no matching function for call to 'RoutineItem::RoutineItem()'

这是 RoutineItem 的构造函数:

RoutineItem(QString Name,int Position,int Time,bool hasCountdown = false,bool fastNext = false);

如果我删除所有构造函数参数,我将不再收到该错误。如何将 QVector 与具有参数的自定义对象一起使用?

最佳答案

问题是 QVector 要求元素有一个默认的构造函数(这是错误信息)。您可以在您的类(class)中定义一个。例如:

class RoutineItem {
RoutineItem(QString Name, int Position,
int Time, bool hasCountdown = false,
bool fastNext = false);
RoutineItem();
[..]
};

或者,您可以让所有参数都具有默认值:

class RoutineItem {
RoutineItem(QString Name = QString(), int Position = 0,
int Time = 0, bool hasCountdown = false,
bool fastNext = false);
[..]
};

或者,您可以构造 RoutineItem 的默认值并通过它初始化所有 vector 项:

RoutineItem item("Item", 0, 0);
// Create a vector of 10 elements and initialize them with `item` value
QVector<RoutineItem> vector(10, item);

关于c++ - 带有参数的自定义对象的 QVector?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24305390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com