C++ “结构迭代器”;节点";不允许数据检索
我正在为一个问题挣扎。我有我们的常规节点结构和一个存储节点指针的列表。当我试图使用迭代器从这样的列表中检索值时,我无法这样做C++ “结构迭代器”;节点";不允许数据检索,c++,C++,我正在为一个问题挣扎。我有我们的常规节点结构和一个存储节点指针的列表。当我试图使用迭代器从这样的列表中检索值时,我无法这样做 #include <list> #include <iostream> using namespace std; struct node { int data; node* next; }; int main() { node * n = new node; n->data = 3; n->
#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"
}
#包括
#包括
使用名称空间std;
结构节点
{
int数据;
节点*下一步;
};
int main()
{
node*n=新节点;
n->data=3;
n->next=NULL;
清单l;
l、 向前推(n);
列表::迭代器myIt=l.begin();
cout datacout data运算符优先级问题:使用cout data您实际上有点困惑,需要执行以下操作
cout << (*myIt)->data << endl;
cout数据您需要了解运算符的先例。另外,为什么要将此标记为c
?指针列表是双间接的,在大多数情况下完全不需要。如果恰好在您的操作中需要它(即多态性启用码),请使用智能指针。
cout << (*myIt)->data << endl;
cout << (*myIt)->data << endl;