结构“节点”的迭代器不允许数据检索

时间:2014-01-18 19:31:01

标签: c++

我正在努力解决一个问题。我有我们的常规节点结构和一个存储节点指针的列表。当我试图使用迭代器从这个列表中检索值时,我无法这样做......

#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"
}

也许我对迭代器的使用感到困惑。你能告诉我一个解决方法吗?

干杯!!!

3 个答案:

答案 0 :(得分:4)

运算符优先级问题:使用cout << (*myIt)->data << endl;

答案 1 :(得分:0)

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

您需要额外的()

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

通过,

弗朗西斯

答案 2 :(得分:-1)

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

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

您首先要取消引用指针,然后才能获取数据。