1

When trying to compile I get the error: Error C2280 'std::variant<Tree::Position<int>,Tree::Position<std::string>,Tree::Position<double>,Tree::Position<bool>>::variant(const std::variant<Tree::Position<int>,Tree::Position<std::string>,Tree::Position<double>,Tree::Position<bool>> &)': attempting to reference a deleted function

I have a Tree class with a template sub-class Position. When using the load function of the Tree class, an instance of the Position class is added to the Tree's treePositionList. Also in the constructor function of the Position class an instance of the Position class is added to its childenList. I think the problem is related to the adding of an instance to those lists, though I do not understand what goes wrong exactly.

template <typename E>
class Node {
private:
    string column;
    E element;
public:
    Node(E el, string col) { element = el; column = col;}
};

class Tree {
public:
    template <typename E>
    class Position {
    private:
        typedef variant<Position<int>, Position<string>, Position<double>, Position<bool>> Position_ManyTypes;
        typedef list<Position_ManyTypes> PositionList;

        Node<E>* node;
        PositionList childrenList;
    public:
        Position(Tree* tree, const E element, const string column, const Json::Value &children); 
        Position(Position<E>& position);

        ~Position();

        Position<E>& operator=(const Position<E> &position);
    friend class Tree;
    };

    typedef variant<Position<int>, Position<string>, Position<double>, Position<bool>> Position_ManyTypes;

    typedef list<Position_ManyTypes> PositionList;
    Tree() {}

    bool load(string filename);
private:
    PositionList treePositionList;
};

bool Tree::load(string filename) {
    ifstream rules_file(filename, ifstream::in);
    Json::Value rules;
    rules_file >> rules;
    rules_file.close();

    Position<string> root_position = Position<string>(this, "string123", "string456", rules["children"]);
    treePositionList.push_back(root_position);
    return true;
}

template <typename E>
Tree::Position<E>::Position(Tree::Position<E>& position) {
    node = position.node;
    childrenList = position.childrenList;
}

template <typename E>
Tree::Position<E>& Tree::Position<E>::operator=(const Tree::Position<E>& position) {
    if (this != &position) {
        delete node;
        node = position.node;
        childrenList = position.childrenList;
    }
    return *this;
}

template <typename E>
Tree::Position<E>::~Position() {
    delete node;
}

template <typename E>
Tree::Position<E>::Position(Tree* tree, const E el, const string col, const Json::Value &children) {
    node = new Node<E>(el, col);

    Position<string> pos = Position<string>(tree, "string123", "string456", children[0]["children"]);
    childrenList.push_back(pos);
}
KobeFl
  • 17
  • 8
  • 1
    I see the likely reason for the compilation error, however the shown code fails to meet all requirements for a [mre], as explained in the [help]; and it's been my experience that, too often, there is a different underlying cause that was not visible because of the code that's not shown, and too often after spending some time constructing an answer, it turned to to be a waste of time because of that. Please [edit] your question and include a self-contained code sample that anyone can cut/paste ***exactly as shown*** and reproduce the same compilation error. See [ask] for more information. – Sam Varshavchik Apr 14 '20 at 02:18
  • Your Position template lacks a copy constructor, so the variant will also lack a copy constructor. It looks like your `Position(Position&)` can be upgraded to a copy constructor by adding`const`. – Raymond Chen Apr 14 '20 at 02:52
  • @RaymondChen Thank you very much. I've been looking over it for hours and I completely missed that. – KobeFl Apr 14 '20 at 11:26
  • "attempting to reference a deleted function" is the clue. The copy constructor was deleted. Why would a variant delete its copy constructor? [Copy constructor. This constructor is defined as deleted unless `std::is_copy_constructible_v` is true for all `T_i` in `Types....`](https://en.cppreference.com/w/cpp/utility/variant/variant) – Raymond Chen Apr 14 '20 at 16:15

0 Answers0