1

尝试编译时出现错误: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

我有一个带有模板子类位置的树类。使用 Tree 类的加载功能时,会将 Position 类的实例添加到 Tree 的treePositionList. 同样在 Position 类的构造函数中,Position 类的一个实例被添加到它的childenList. 我认为问题与向这些列表中添加实例有关,尽管我不明白到底出了什么问题。

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);
}
4

0 回答 0