No appropriate default constructor available when defining a struct inside QMap
-
wrote on 26 Jul 2020, 12:50 last edited by lansing
I have a list of one key multiple values to insert into a container.
id: "png", description: "this is png", quality: 100 id: "jpg", description: "this is jpg", quality: 0
id
is the key anddescription
andquality
are the values. I tried to do this withQMap
with a struct at the value positionstruct ImageProp { QString description; int quality; ImageProp(QString d, int qu) : description(d), quality(qu) {} }; typedef QMap<QString, ImageProp> ImageFormat; ImageFormat imageFormatMap; imageFormatMap.insert("png", ImageProp("this is png", 100));
The editor detected no error, but when I ran it I got the errors:
'MyClass::ImageProp' no apprpriate default constructor available C:\Qt\5.15.0\msvc2019_64\include\QtCore\qmap.h:679: error: C2664: 'void QMap<QString, MyClass::ImageProp>::insert(const QMap<QString, MyClass::ImageProp> &)': cannot convert argument 1 from 'const Key' to 'const QMap<QString,MyClass::ImageProp> &' with [ Key=QString ]
For some reason it's not reading my constructor.
-
Hi,
A default constructor is a constructor that requires no parameters to be used. So it either have no parameters or all parameters have a default value.
-
Hi,
A default constructor is a constructor that requires no parameters to be used. So it either have no parameters or all parameters have a default value.
-
Thanks, adding a blank constructor for the struct solved it.
struct ImageProp { QString description; int quality; ImageProp() {} ImageProp(QString d, int qu) : description(d), quality(qu) {} };
Lifetime Qt Championwrote on 26 Jul 2020, 17:13 last edited by Christian Ehrlicher@lansing This leaves quality uninitialzed...
This is much better, it also avoids one useless copy
struct ImageProp { QString description; int quality = 0; ImageProp() = default; ImageProp(const QString &d, int qu) : description(d), quality(qu) {} };
-
@lansing This leaves quality uninitialzed...
This is much better, it also avoids one useless copy
struct ImageProp { QString description; int quality = 0; ImageProp() = default; ImageProp(const QString &d, int qu) : description(d), quality(qu) {} };
wrote on 26 Jul 2020, 17:27 last edited byThanks I'll do that. And why the use of
QString &d
instead ofQString d
in the argument? -
@lansing said in No appropriate default constructor available when defining a struct inside QMap:
And why the use of QString &d instead of QString d in the argument?
To avoid a copy of the QString object
-
Thanks I'll do that. And why the use of
QString &d
instead ofQString d
in the argument?@lansing said in No appropriate default constructor available when defining a struct inside QMap:
Thanks I'll do that. And why the use of
QString &d
instead ofQString d
in the argument?The const is important in const QString &
-
@lansing said in No appropriate default constructor available when defining a struct inside QMap:
And why the use of QString &d instead of QString d in the argument?
To avoid a copy of the QString object
wrote on 26 Jul 2020, 23:33 last edited byOkay I got it. I'll use
const QString &
from now on.
1/8