提问者:小点点

使用类指针作为值遍历映射元素时出错


我有一个课要用在trie上

class Node {
public:
    map<char,Node*> node;
    bool flag = false;
};

int main(){
    Node table;
    for(auto x: table){}
    return 0;
}

我试着遍历它的元素,但是我总是得到错误

我检查了stack中的解决方案,并尝试使用迭代器映射方法:

error: 'class Node' has no member named 'begin'
for (it = table->begin(); it != table->end(); it++){
                ^~~~~
error: 'class Node' has no member named 'end'
    for (it = table->begin(); it != table->end(); it++){

也尝试使用for循环,得到了类似的错误:

error: 'begin' was not declared in this scope
for(auto x: table){
            ^~~~

如何以另一种方式迭代元素,或者如何修复这些错误?


共1个答案

匿名用户

不是可迭代的类。它只是一个常规类,碰巧有一个作为成员。您需要访问Node中的对象:

node)/code>

另外,在您展示给我们的代码中,从未初始化。我假设在您的实际代码中,它是,但如果不是,也修复它。您可以使用创建对象,或者将其声明为值而不是指针: