Passing Iterator of a class to another list of class in C++

50 Views Asked by At

I tried to make a tree class consists of "Node Structure" which contain a list of pointers for type "Node" and made a "Position class" to act as a dynamic pointer to it and "PositionList" a list of type "Position"


I made a list of type "Node" in Node Structure and children function which make a temp positionList and iterate on childList and push iterator into temp positionList

class Tree {
    class Position;
    typedef list<Position> PositionList;
protected:
    // Node Structure
    struct Node {
        Elem elem;
        Node* par;
        list<Node> childList;
        Node() : elem(), par(NULL), childList(NULL) {}
    };
public:
    // Position Class
    class Position {
    private:
        Node* node;
    public:
        Position(Node* _node = NULL) :node(_node) {}
        Elem& operator*() { return node->elem; }
        Position parent() const { return node->par; }
        PositionList children() const {
            PositionList posList;
            for (list<Node>::iterator iter = node->childList.begin();iter != node->childList.end(); ++iter) {
                posList.push_back(iter);
            }
        }
}
    };

};

this code give an error Message

C++ no instance of overloaded function matches the argument list argument types are: (Tree::Node) object type is: Tree::PositionList

But when I change the childList list type from Node to Node* and deref the iterator it worked well

struct Node {
    list<Node*> childList;
}
...
for (list<Node>::iterator iter = node->childList.begin();iter != node->childList.end(); ++iter) {
    posList.push_back(*iter);
}
0

There are 0 best solutions below