我定义了个结构体:
typedef struct student
{
   CString csCode;      
   CString csName;      
   CString csSex;      
   CString csAge;       
   CString csDept;
   struct student *next;
}student,*linklist;
然后在MFC里面给结构体的变量赋值:CString str("test");
structStudent[1].csCode=  str;可是会出问题:
Debug Assertion Failed!
Expression:_CrtIsValidHeapPointer(pUserData)调试时问题出在CString的赋值函数里面:const CString& CString::operator=(const CString& stringSrc)
{
if (m_pchData != stringSrc.m_pchData)//DEBUG调试停在这里
{
难道我这样赋值不对吗??
请各位大牛指教!!!

解决方案 »

  1.   

     这样赋值理论上没有错的啊       CString str("test"); 
           structStudent[1].csCode=  str; 其中structStudent 是个什么样的数组?
    是new分配的吗?
    有没有初始化?
      

  2.   

    关键是你怎么声明的structStudent.下面这种做法就没问题 student structStudent[10];
            
     CString str("test"); 
     structStudent[1].csCode=  str; 
      

  3.   

    结构体不要用CString
    那个是对象``````````````
    用char * 或者[]
    ```````
      

  4.   

    改CString *pcsCode;      
      

  5.   

    struct student里的CString成员没有初始化这里要用new来分配struct student的对象空间
    这样CString成员才会执行构造函数进行初始化
      

  6.   

    struct student里最好别用不定长数据类型,否则很容易出错!
    使用指针,是最好的选择.student structStudent[10];
            
     CString str("test"); 
     structStudent[1].csCode=  str
    是因为你先确定了数组的大小,而 CString本身是一个char* 数组
    如果是你使用CArray时,使用Add的方法加入数据,肯定挂.
    可先SetSize,然后在调用,就没问题.
      

  7.   

    结构体中,最好不要放 不定长的数据,你其实可以写成一个类的,把这些Cstring变量 放在类中,调用起来 也挺方便的。
      

  8.   

    结构体一般是不用CString的用得多的是 char szXXX[20]
      

  9.   

    structStudent[1].csCode.Format("%s",str);