vendredi 28 juin 2019

Why compiler implicitly deletes constructor while trying to initialize an array of objects with C++11 style

I'm trying to write a very simple constructor init list but falling short on an array of objects. The compiler says:

...parent-class.cpp:5: error: use of deleted function ‘SubClass::SubClass(SubClass&&)’
     , subObjects{ {this} }
                    ^

I'm sure this is a basic concept about Modern C++ and seen many answered questions around. But none of them clarified what I'm missing.

Here's the basic code which creates this compiler error (which is g++ 8.3.0)

ParentClass declaration:

#ifndef PARENTCLASS_H
#define PARENTCLASS_H

#include <QObject>
#include "sub-class.h"

class ParentClass : public QObject
{
    Q_OBJECT
public:
    explicit ParentClass(QObject *parent = nullptr);

private:
    SubClass subObjects[3];
};

#endif // PARENTCLASS_H

Parent Class Implementation:

#include "parent-class.h"

ParentClass::ParentClass(QObject *parent)
    : QObject(parent)
    , subObjects{ {this} }
{
}

Sub Class Decleration:

#ifndef SUBCLASS_H
#define SUBCLASS_H

#include <QObject>

class SubClass : public QObject
{
    Q_OBJECT
public:
    SubClass(QObject *parent = nullptr);
};

#endif // SUBCLASS_H

SubClass Implementation:


#include "sub-class.h"
SubClass::SubClass(QObject *parent)
    : QObject(parent)
{
}

Creating a dynamic array could be a workaround but I'm trying to adapt to Modern C++. Since I'm mostly an embedded guy, dynamic arrays are also out of question many times.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire