Use of delete function - Inheritance of a QList

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;

BaseClass

Header

#ifndefCOMPONETNUTRITIONAL#defineCOMPONETNUTRITIONAL#include<QObject>#include<QList>classComponentNutritional:publicQObject{Q_OBJECTpublic:explicitComponentNutritional(QObject*parent=0);~ComponentNutritional();QStringgetName();floatgetValue();floatgetDairyValue();QStringgetMeasure();protected:QStringname;floatvalue;floatdairyvalue;QStringmeasure;};#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;
}

Inheriting class

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

    
asked by anonymous 08.06.2016 / 15:57

1 answer

1

According to documentation :

  

QList's value type must be assignable data type.

That is, for a QList<T> , T must be assignable , this is :

  

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

That is, T must be copied or provide a copy constructor. Classes derived from QObject explicitly exclude the copy constructor, causing, in your case, QList<Vitamin> to try to use a constructor that does not exist for the deleted type. Since pointers, being primitive values, can be copied; so QList<Vitamin *> works.

    
10.06.2016 / 05:56