Home > Mobile >  why does the compiler declare a class method deleted?
why does the compiler declare a class method deleted?

Time:12-12

i'm trying to make a class which include std::pair container, see the code bellow.

  • why did the compiler declare a class method deleted?
  • how to fix this issue?
template <typename Kty_, typename Dty_>
class TreeNode {
public:

    // tags:
    using usI             = unsigned short int;                    
    using value_type      = std::pair<const Kty_, Dty_>;           
    using pointer         = value_type*;  
    using reference       = value_type&; 
    using const_reference = const value_type&; 



    TreeNode(
        const_reference             pairValue,
        usI                         height  = 0,
        TreeNode<const Kty_, Dty_> *parent  = nullptr,
        TreeNode<const Kty_, Dty_> *left    = nullptr,
        TreeNode<const Kty_, Dty_> *right   = nullptr
    ) {
        left_       = left;
        parent_     = parent;
        right_      = right;
        pairValue_  = pairValue;  // 48 // Overload resolution selected deleted operator '=' ​clang:ovl_deleted_oper
        height_     = height;
    }
protected:
    TreeNode<const Kty_, Dty_> *left_;
    TreeNode<const Kty_, Dty_> *parent_;
    TreeNode<const Kty_, Dty_> *right_;
    
    value_type                  pairValue_;
    usI                         height_;
};
  • there is a set of examples which call this issue:
    • Example: Uninitialized data members: it doesn't work for me because i don't know how to initialize Kty_ & Dty_ by their default values
    • Example: Reference and const data members: i have tried to remove some of const keyword specifies, but it doesn't help
    • Example: Movable deletes implicit copy: not my case i think
    • Example: Indirect base members deleted: not my case i think
    • Example: Variant and volatile members: not my case i think

CodePudding user response:

why did the compiler declare a class method deleted?

Because const members cannot be assigned to, value_type pairValue_; contains a const member.

how to fix this issue?

Initialize const members properly.

One cannot assign to constant members in the constructor's body, which is what you were doing, just move the "initialization" (or rather all of them) to the initializer list.

TreeNode(const_reference pairValue, usI height = 0,
         TreeNode<const Kty_, Dty_> *parent = nullptr,
         TreeNode<const Kty_, Dty_> *left = nullptr,
         TreeNode<const Kty_, Dty_> *right = nullptr)
    : left_(left),
      parent_(parent),
      right_(right),
      pairValue_(pairValue),
      height_(height) {}
  • Related