结构"node"的迭代器不允许数据检索

Iterator for structure "node" is not allowing data retrieval

本文关键字:数据 检索 不允许 node 结构 迭代器      更新时间:2023-10-16

我正在努力解决问题。我有我们的常规节点结构和存储节点指针的列表。当我尝试使用迭代器来检索该列表时,我无法做到...

#include <list>
#include <iostream>
using namespace std;
struct node
{
    int data;
    node* next;
};
int main()
{
    node * n = new node;
    n->data = 3;
    n->next = NULL;
    list<node*> l;
    l.push_front(n);
    list<node*>::iterator myIt = l.begin();
    cout << *myIt->data << endl;   // <-- the compiler shows an error here "Member reference base type "node*" is not a structure or union"
}

也许我对迭代器的用法感到困惑。你能建议我解决方法吗?

欢呼!!

操作员优先级问题:使用cout << (*myIt)->data << endl;

  cout << *myIt->data << endl;

您需要添加()

  cout << (*myIt)->data << endl;

再见,

弗朗西斯

您实际上有点困惑,您需要做以下

cout << (*myIt)->data << endl;

您首先要取消指针,然后可以获取数据。