Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVector with custom objects that have arguments?

I`m trying to use a QVector with a custom object named RoutineItem.

But this error is given:

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

This is the RoutineItem constructor:

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

If I remove all the constructor arguments I no longer get that error. How can I use QVector with a custom object that has arguments?

like image 913
Scott Avatar asked Jan 28 '26 05:01

Scott


1 Answers

The problem is that QVector requires that the element has a default constructor (that is the error message about). You can define one in your class. For example:

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

Alternatively, you can let all arguments have a default values:

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

Alternatively, you can construct a default value of RoutineItem and initialize all vector items by it:

RoutineItem item("Item", 0, 0);
// Create a vector of 10 elements and initialize them with `item` value
QVector<RoutineItem> vector(10, item);
like image 71
vahancho Avatar answered Jan 29 '26 20:01

vahancho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!