简体   繁体   中英

C++ - iterator for a vector of objects not compiling

I have a class Tree and I'm trying to iterate through a list of them and print the data member of that class, I have the following two methods in the class:

Node definition within Tree :

class Node {
    public:
        int data;
        Node const* left;
        Node const* right;
};

Snippet of the writeSets method:

void Tree::writeSets(std::vector<Node const*> &list, Node const* root) {
    if (root == NULL) {
        return;
    }

    // Displays all preceding left values
    for (std::vector<Node const*>::iterator it = list.begin(); it != list.end(); it++) {
        std::cout << *it->data;
        *** Getting compile error here **
    }
}

Compile error is:
: error: request for member 'data' in '* it. __gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> with _Iterator = const Tree::Node**, _Container = std::vector >', which is of non-class type 'const Tree::Node*'

data within the Node object is of type Int , not sure if I need to define the expected datatype of the iterator as well as the datatype that the vector is wrapping to overcome this error.

Would appreciate someones help here.

Many thanks.

You should write (*it)->data .

*it->data is equivalent to *(it->data) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM