i have a struct "Material" which has a referencetype of Item& Item is a baseclass for many different Materials who can appear in a list. The struct also has an integer variable and also a QString variable. Those two are just give the amount to be used and a String of what type item must be casted back.
struct Material
{
int amount;
Item& item;
QString itemtype;
Material(int myamount,Item& myitem,QString myitemtype) : amount(myamount),item(myitem),itemtype(myitemtype){}
};
As i have read here: initialize struct contain references to structs
to have a reference sitting inside of a struct one need to define a constructor within the struct. as you can see in the upper Material struct.
Now when i try to retrive the reference of that, like here:
QList<Material> Mats = bp.Materials();
for(int i=0;i<Mats.count();i++)
{
Item& item = Mats[i].item;
}
i allways get an error like "use of deleted function 'Material& Material::operator=(const Material&)"
I also tried already to define such a operator= but obviously if i try to return the rvalue which is const i can't do this because the lvalue is not const. and if i make a new instance of Material it is going to be an implicit copy constructor.
My Question: What do i miss here?
On the wish to make a reproduceable example, i may got to the problem. I have no solution for my one just yet but an idea what may have caused this:
#include <QCoreApplication>
#include <QList>
struct Kram
{
QString& text;
Kram(QString& s): text(s) {}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString mytext= "Hello, World!";
Kram k(mytext);
QList<Kram> liste;
liste.append(k);
for(int i=0; i<liste.count();i++)
{
QString& anothertext = liste[i].text;
}
return a.exec();
}
When i compile this he tells me the exact same error message about the "Kram" struct. But this only appears if you do the struct in a Qlist and then try to get the reference inside of the struct out.
just to mention the second error message is in both cases thisone:
C:\Users\Rolf\Documents\Testprojekt\main.cpp:4: Fehler: non-static reference member 'QString& Kram::text', can't use default assignment operator
bp.Materials().values()
return? If it's a reference or const reference then this is the type you have to use for your local variableMats
. Otherwise, you're lost. As you have it currently, you copy theQList<Material>
, and this seems to make a deep copy (copying all the list elements as well).Item
instances that you keep references to?struct
with a reference member. In this case, you have to turn it into a pointer or try to prevent any copy assignment. (Sometimes, this can be intended e.g. for resource management.)QList
must be able to perform a deep copy (when copy-on-write happens). Hence, there is in fact somewhere in theQList
a copy assignment used for the elements (of typeMaterial
in your case). And this doesn't compile as the compiler cannot create one for yourMaterial
due to theMaterial::item
member which is a reference.