我有一个类Tree,我正在尝试遍历它们的列表并打印该类的data
成员,我在类中有以下两种方法:
Node
中的 Tree
定义:
class Node {
public:
int data;
Node const* left;
Node const* right;
};
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 **
}
}
编译错误是:
:错误:请求'*它中的成员'数据'。 __gnu_cxx :: __ normal_iterator&lt; _Iterator,_Container&gt; :: operator-&gt; with _Iterator = const Tree :: Node **,_Container = std :: vector&gt;',这是非类型'const Tree :: Node *'
data
对象中的 Node
类型为Int
,不确定是否需要定义迭代器的预期数据类型以及向量包装以克服的数据类型这个错误。
非常感谢有人帮忙。
非常感谢。
答案 0 :(得分:3)
你应该写(*it)->data
。
*it->data
相当于*(it->data)
。