自定义的node struct list访问出错,该怎么处理
自定义的node struct list访问出错
#include "stdafx.h"
#include <list>
using namespace std;
struct node
{
int a;
void* ptr;
}T_Node;
typedef list<node *> T_MYLIST;
int _tmain(int argc, _TCHAR* argv[])
{
T_MYLIST mylist;
node *pNode = new node;
mylist.push_back(pNode);
T_MYLIST::iterator it = mylist.begin();
int num = it->a;
return 0;
}
报错如下
error C2839: 重载的“operator ->”的无效返回类型“node **”
error C2039: “a”: 不是“std::list<_Ty>::_Iterator<_Secure_validation>”的成员
------解决思路----------------------
先搞清楚你的it指向什么。
int num = (*it)->a;
------解决思路----------------------
(*it)->a
*it 才是 mylist 的元素, Node *
#include "stdafx.h"
#include <list>
using namespace std;
struct node
{
int a;
void* ptr;
}T_Node;
typedef list<node *> T_MYLIST;
int _tmain(int argc, _TCHAR* argv[])
{
T_MYLIST mylist;
node *pNode = new node;
mylist.push_back(pNode);
T_MYLIST::iterator it = mylist.begin();
int num = it->a;
return 0;
}
报错如下
error C2839: 重载的“operator ->”的无效返回类型“node **”
error C2039: “a”: 不是“std::list<_Ty>::_Iterator<_Secure_validation>”的成员
------解决思路----------------------
先搞清楚你的it指向什么。
int num = (*it)->a;
------解决思路----------------------
(*it)->a
*it 才是 mylist 的元素, Node *