Use of delete Function - Inheritance of a Qlist

Asked

Viewed 139 times

1

When compiling I get the following error, in Qlist. I’m using Qt 5.6.

error: use of Deleted Function 'Vitamin::Vitamin(const Vitamin&)' Current->v = new T(*reinterpret_cast(src->v));

error: use of Deleted Function 'Vitamin::Vitamin(const Vitamin&)' new (Current) T(*reinterpret_cast(src));

error: use of Deleted Function 'Vitamin::Vitamin(const Vitamin&)' Else if (Qtypeinfo::isComplex) new (n) T(t);

error: use of Deleted Function 'Vitamin& Vitamin::Operator=(const Vitamin&)' Else *reinterpret_cast(n) = t;

inserir a descrição da imagem aqui

Base Class

Header


#ifndef COMPONETNUTRITIONAL
#define COMPONETNUTRITIONAL

#include <QObject>
#include <QList>

class ComponentNutritional:public QObject
{
    Q_OBJECT
public:
    explicit ComponentNutritional(QObject *parent = 0);
    ~ComponentNutritional();

    QString getName();
    float getValue();
    float getDairyValue();
    QString getMeasure();

protected:
    QString name;
    float value;
    float dairyvalue;
    QString measure;

};

#endif // COMPONETNUTRITIONAL

Cpp


#include "componetnutritional.h"

#include <typeinfo>

ComponentNutritional::ComponentNutritional(QObject *parent):QObject(parent)
{
    this->name = typeid(this).name();
    this->dairyvalue = 0;
    this->measure = "";
    this->value = 0;
}

ComponentNutritional::~ComponentNutritional()
{

}

QString ComponentNutritional::getName()
{
    return this->name;
}

float ComponentNutritional::getValue()
{
    return this->value;
}

float ComponentNutritional::getDairyValue()
{
    return this->dairyvalue;
}

QString ComponentNutritional::getMeasure()
{
    return this->measure;
}

Class that achieves inheritance

Header


#ifndef VITAMIN_H
#define VITAMIN_H

#include <QObject>
#include "Library/Types/Base/componetnutritional.h"

class Vitamin : public ComponentNutritional
{
public:
    explicit Vitamin(QObject *parent = 0);
    ~Vitamin();
    Q_INVOKABLE  Vitamin *setName(QString value);
    Q_INVOKABLE  Vitamin *setValue(float value);
    Q_INVOKABLE  Vitamin *setDairyValue(float value);
    Q_INVOKABLE  Vitamin *setMeasure(QString value);
};

class Vitamins: public QList<Vitamin>{
};
#endif // VITAMIN_H

Cpp


#include "vitamin.h"

Vitamin::Vitamin(QObject *parent): ComponentNutritional(parent)
{

}

Vitamin::~Vitamin()
{

}

Vitamin *Vitamin::setName(QString value)
{
    this->name = value;
    return this;
}

Vitamin *Vitamin::setValue(float value)
{
    this->value = value;
    return this;
}

Vitamin *Vitamin::setDairyValue(float value)
{
    this->dairyvalue = value;
    return this;
}

Vitamin *Vitamin::setMeasure(QString value)
{
    this->measure = value;
    return this;
}

Solution


#ifndef VITAMIN_H
#define VITAMIN_H

#include <QObject>
#include "Library/Types/Base/componetnutritional.h"

class Vitamin : public ComponentNutritional
{
public:
    explicit Vitamin(QObject *parent = 0);
    ~Vitamin();
    Q_INVOKABLE  Vitamin *setName(QString value);
    Q_INVOKABLE  Vitamin *setValue(float value);
    Q_INVOKABLE  Vitamin *setDairyValue(float value);
    Q_INVOKABLE  Vitamin *setMeasure(QString value);
};

class Vitamins: public QList<Vitamin*>{ //Alterado para ponteiro
};
#endif // VITAMIN_H

  • Man, you could wipe that question down and leave just a minimal example, but demonstrate your problem.

1 answer

1


According to the documentation:

Qlist’s value type must be an Assignable data type.

That is, for a QList<T>, T has to be Assignable, that is to say:

To qualify, a type must provide a default constructor, a copy constructor, and an assignment Operator.

That is, the T must be copyable or provide a copy constructor. Classes derived from QObject explicitly exclude the copy constructor, in your case, by which QList<Vitamin> try using a constructor that does not exist for the type (Deleted). Pointers, because they are primitive values, can be copied; therefore, QList<Vitamin *> works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.