CTypedPtrList<CPtrList,CPic*> m_Lines;//这是定义的链表对象CPic是自己定义的类。
POSITION pos;
pos=m_Lines.GetHeadPosition();
CPic * temp;
while(pos)
{
temp=m_Lines.GetNext(pos);
CString tem;
tem.Format("%d",temp->m_Width);
AfxMessageBox(tem);//检测遍厉所取得的值
}结果发现每次取得的都是链表的最后一个节点的值,虽然链表所有的节点都遍厉过了,不知道是何原因!
POSITION pos;
pos=m_Lines.GetHeadPosition();
CPic * temp;
while(pos)
{
temp=m_Lines.GetNext(pos);
CString tem;
tem.Format("%d",temp->m_Width);
AfxMessageBox(tem);//检测遍厉所取得的值
}结果发现每次取得的都是链表的最后一个节点的值,虽然链表所有的节点都遍厉过了,不知道是何原因!
{
public:
int m_Width;
};
#include <afxtempl.h>
//...
CTypedPtrList<CPtrList,CPic*> m_Lines;//这是定义的链表对象CPic是自己定义的类。
void CListDlg::OnButton1()
{
CPic *pPic1 = new CPic;
pPic1->m_Width = 0;
m_Lines.AddHead(pPic1); CPic *pPic2 = new CPic;
pPic2->m_Width = 1;
m_Lines.AddHead(pPic2); CPic *pPic3 = new CPic;
pPic3->m_Width = 2;
m_Lines.AddHead(pPic3); //演示用,没用delete
}void CListDlg::OnButton2()
{
POSITION pos;
pos=m_Lines.GetHeadPosition();
CPic * temp;
while(pos)
{
temp=m_Lines.GetNext(pos);
CString tem;
tem.Format("%d",temp->m_Width);
AfxMessageBox(tem);//检测遍厉所取得的值
}
}
结果2,1,0
temp=m_Lines.GetNext(pos);
上面加上一句:
temp = new CPic();