简体   繁体   English

C ++-不编译对象向量的迭代器

[英]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: 我有一个类Tree,并且试图遍历它们的列表并打印该类的data成员,该类中有以下两个方法:

Node definition within Tree : Tree Node定义:

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

Snippet of the writeSets method: writeSets方法的代码段:

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*' __gnu_cxx :: __ normal_iterator <_Iterator,_Container> :: operator->与_Iterator = const Tree :: Node **,_Container = std :: vector>',这是非类类型的'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. data的内部Node对象类型的Int ,不知道如果我需要定义迭代器的预期数据类型以及该载体包裹克服这种错误的数据类型。

Would appreciate someones help here. 希望有人在这里提供帮助。

Many thanks. 非常感谢。

You should write (*it)->data . 您应该写(*it)->data

*it->data is equivalent to *(it->data) . *it->data等效于*(it->data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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