建立了一个结构体,想用它保存列表控件List Control中的内容,然后在下拉列表控件Combo Box中读取结构体中一个变量的值,也就是列表控件中对应的一列。编译过了,但是运行保存的时候,提示:“ox5f47720c”指令引用的“oxccccccd4”内存,该内存不能为“read”。不知道原因,把代码贴出来,请大家帮忙看看://头文件中定义结构体
public:
typedef struct PartAnalysis
{
CString PartID;
CString PartName;
CString GivenName;
CString AF;
CString Type;
}ProductInfo;
ProductInfo *productinfo;//实现文件中,把列表控件内容保存到结构体中,列表控件相关变量m_listPA
void CTestDlg::OnButtonPaSave() 
{
        int nItemCount=m_listPA.GetItemCount(); 
for(int i=0;i<nItemCount;i++)
{
productinfo->PartID = m_listPA.GetItemText(i,0);
productinfo->PartName = m_listPA.GetItemText(i,1);
productinfo->GivenName = m_listPA.GetItemText(i,2);
productinfo->AF = m_listPA.GetItemText(i,3);
productinfo->Type = m_listPA.GetItemText(i,4);
}
        //向下拉列表中添加GivenName这一列,下拉列表关联变量m_cmbASG_PS
        m_cmbASG_PS.AddString(productinfo->GivenName);}

解决方案 »

  1.   

    int nItemCount=m_listPA.GetItemCount(); 
    productinfo = new ProductInfo [nItemCount]; 
    for(int i=0;i<nItemCount;i++)
    {
    productinfo[i].PartID = m_listPA.GetItemText(i,0);
    prproductinfo[i].PartName = m_listPA.GetItemText(i,1);
    productinfo[i].GivenName = m_listPA.GetItemText(i,2);
    productinfo[i].AF = m_listPA.GetItemText(i,3);
    productinfo[i].Type = m_listPA.GetItemText(i,4);m_cmbASG_PS.AddString(productinfo[i].GivenName);}
    delete [] productinfo ;
      
      

  2.   


    ok,但是还有个问题,添加到下拉列表中的项有重复,怎么删除重复的啊,
    比如列表那一列里面是P1,P2,P2,P3,P3,P3,P4,P5,P5,P6,
    用上面的代码后,这些都加进去了,我只希望显示P1,P2,P3,P4,P5,P6,去掉重复的项,怎么操作啊?再谢!
      

  3.   

    都没new出来,就用了非法指针操作了
      

  4.   

    那你要自己比较啊
    把代码中的m_cmbASG_PS.AddString(productinfo[i].GivenName);
    替换成:
    int num = m_cmbASG_PS.GetCount();
    CString str;
    int j=0;
    for(j=0; j<num; j++)
    {
    GetLBText(j,str);
    if(!strcmp(str,productinfo[i].GivenName))
    break;
    }if(j>=num)
    m_cmbASG_PS.AddString(productinfo[i].GivenName);