Reputation: 567
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.
Upvotes: 0
Views: 1137
Reputation: 23058
You should write (*it)->data
.
*it->data
is equivalent to *(it->data)
.
Upvotes: 3