dimanche 16 août 2020

Assign object to a member variable in C++ class constructor

I'm getting

error: use of deleted function ‘PropertyLink& PropertyLink::operator=(PropertyLink&&)’

When I try to assign an object to a member variable in the class constructed.

I have 2 class definitions, PropertyLink and NodeBlock. and I want to create an object of PropertyLink inside NodeBlock when constructing a NodeBlock object.

Following are the class definitions

PropertyLink.h

class PropertyLink {
    
   public:
    PropertyLink(unsigned int);
    PropertyLink();
};

PropertyLink.cpp

#include "PropertyLink.h"

PropertyLink::PropertyLink(unsigned int propertyBlockAddress): blockAddress(propertyBlockAddress) {};
PropertyLink::PropertyLink(): blockAddress(0) {};

NodeBlock.h

#include "PropertyLink.h"
class NodeBlock {
   public:
    PropertyLink properties;

    NodeBlock(unsigned int propRef) {
        properties = PropertyLink(propRef);
        // properties(propRef); // error: no match for call to ‘(PropertyLink) (unsigned int&)’
        // properties = new PropertyLink(propRef); // tried this too none of them worked
    };
};

In the compiler output, there is a note saying

note: ‘PropertyLink& PropertyLink::operator=(const PropertyLink&)’ is implicitly deleted because the default definition would be ill-formed:
[build]     9 | class PropertyLink {

Aucun commentaire:

Enregistrer un commentaire