我使用了控件Edit Box,初始化时,如果是数字则出错,若是CString类型的则正确,变量的定义代码如下:绿色的程序自带的注释,红色的是定义的枚举变量,奇怪的是将枚举变量的位置稍微交换,编译连接没错,但运行时会出现什么内存只能READ之类的。 #if !defined(AFX_SDMOPTION_H__59AA650F_8809_43F3_B133_6B139569E8C9__INCLUDED_) 
#define AFX_SDMOPTION_H__59AA650F_8809_43F3_B133_6B139569E8C9__INCLUDED_ #if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
// SDMOption.h : header file 
// 
#include <vector> 
using namespace std; ///////////////////////////////////////////////////////////////////////////// 
// CSDMOption dialogclass

 CSDMOption : public CDialog 

// Construction 
public: 
CString Int2CString(int i); 
CString m_sTem; 
int m_nItem; vector <CString> m_vsDM; 
vector <CString> m_vsOption; CSDMOption(CWnd* pParent = NULL);  // standard constructor // Dialog Data 
//{{AFX_DATA(CSDMOption) 

enum { IDD = IDD_SETDMOPTION }; 
CListCtrl m_lctrDisp; 
int m_nDM; 
CString m_sItem;
 
//}}AFX_DATA 
// Overrides 
// ClassWizard generated virtual function overrides 
//{{AFX_VIRTUAL(CSDMOption) protected: 

virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support 
//}}AFX_VIRTUAL 
// Implementation 
protected: // Generated message map functions 
//{{AFX_MSG(CSDMOption)[/color]
 virtual BOOL OnInitDialog(); 
afx_msg void OnSet1(); 
afx_msg void OnClickDisp(NMHDR* pNMHDR, LRESULT* pResult); 
afx_msg void OnSet2(); 
virtual void OnOK(); 
//}}AFX_MSG 
DECLARE_MESSAGE_MAP() 
}; //{{AFX_INSERT_LOCATION}} 
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
 
#endif // !defined(AFX_SDMOPTION_H__59AA650F_8809_43F3_B133_6B139569E8C9__INCLUDED_) 下边是初始化的代码,红色代码为运行时出错处。 
// SDMOption.cpp : implementation file 
// 

#include "stdafx.h" 
#include "mrsc.h" 
#include "SDMOption.h" #ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif ///////////////////////////////////////////////////////////////////////////// 
// CSDMOption dialog 

CSDMOption::CSDMOption(CWnd* pParent /*=NULL*/
: CDialog(CSDMOption::IDD, pParent) 

//{{AFX_DATA_INIT(CSDMOption) 
        m_nDM = 0              //本来int型的m_nDM=0应使edit box初始显示0,若m_nDM=3,则初始显示3,但是现在不管为多少 
         m_sItem = _T("");      //它都显示-842150451但是CString型的m_sItem=_T(""); 显示为空却是正确的 

      //}}AFX_DATA_INIT 

void CSDMOption::DoDataExchange(CDataExchange* pDX) 

CDialog::DoDataExchange(pDX); 
//{{AFX_DATA_MAP(CSDMOption) 
DDX_Control(pDX, IDC_DISP, m_lctrDisp); 
DDX_Text(pDX, IDC_DMNUMBER, m_nDM); 
DDV_MinMaxInt(pDX, m_nDM, 1, 999999); 
DDX_Text(pDX, IDC_ITEM, m_sItem); 
//}}AFX_DATA_MAP 

BEGIN_MESSAGE_MAP(CSDMOption, CDialog) 
//{{AFX_MSG_MAP(CSDMOption) 
ON_BN_CLICKED(IDC_SET1, OnSet1) 
ON_NOTIFY(NM_CLICK, IDC_DISP, OnClickDisp) 
ON_BN_CLICKED(IDC_SET2, OnSet2) 
//}}AFX_MSG_MAP 
END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// 
// CSDMOption message handlers
BOOL CSDMOption::OnInitDialog() 

CDialog::OnInitDialog(); // TODO: Add extra initialization here 
m_lctrDisp.InsertColumn(0, " ",LVCFMT_LEFT, 120, 0); // 添加列 
m_lctrDisp.InsertItem(0, "DM name"); // 添加行 
m_lctrDisp.InsertItem(1, "Option Number");  // 添加内容 for(int i=1; i <=m_nDM; i++) 

m_lctrDisp.InsertColumn(i," ", LVCFMT_LEFT, 100, 0); 
m_lctrDisp.SetItemText(0, i, m_vsDM[i-1]); 
m_lctrDisp.SetItemText(1, i, m_vsOption[i-1]); 

UpdateData(false); 
return TRUE;  // return TRUE unless you set the focus to a control 
              // EXCEPTION: OCX Property Pages should return FALSE}
 void CSDMOption::OnSet1() 

// TODO: Add your control notification handler code here 
int n=m_nDM; 
UpdateData(true); 
if(m_nDM != n) 

for(int i=1; i <=n; i++) 

m_lctrDisp.DeleteColumn(1);    
} for(i=1; i <=m_nDM; i++) 

m_lctrDisp.InsertColumn(i, " ",LVCFMT_LEFT, 100, i); m_sTem.Empty(); 
m_sTem = Int2CString(i); 
m_sTem.Insert(0, "DM"); 
m_lctrDisp.SetItemText(0, i, m_sTem); m_lctrDisp.SetItemText(1, i, "0"); 


UpdateData(false); 
} CString CSDMOption::Int2CString(int i) 

CString cs; 
again: cs.Insert(0, (char)(i%10+48)); 
if(i=i/10) goto again; return cs; 
} void CSDMOption::OnClickDisp(NMHDR* pNMHDR, LRESULT* pResult) 

// TODO: Add your control notification handler code here 
int n = m_lctrDisp.GetSelectionMark(); 
if(n != -1) 

m_nItem = n; 
m_sItem.Empty(); 
for(int i=1; i <=m_nDM; i++) 

m_sItem += m_lctrDisp.GetItemText(m_nItem, i); 
m_sItem += ","; 


UpdateData(false); 
*pResult = 0; 
} void CSDMOption::OnSet2() 

// TODO: Add your control notification handler code here UpdateData(true); 
int n = 0,  =  m_sItem.Find(','); 
for(; !=-1; ) 

n += 1; 
 = m_sItem.Find(',', +1); 

if(n != m_nDM) return;                      
int n1=0, n2=0, j; 
for(int i=1; i <=m_nDM; i++) 

n2 = m_sItem.Find(',', n1); 
m_sTem.Empty(); 
for(j=n1; j <n2; j++) 

m_sTem += m_sItem[j]; 

m_lctrDisp.SetItemText(m_nItem, i, m_sTem); 
n1 = n2+1; 
} m_nItem = -1; 
m_sItem.Empty(); 
UpdateData(false); 
} void CSDMOption::OnOK() 

// TODO: Add extra validation here 
m_vsDM.clear(); 
m_vsOption.clear(); 
for(int i=1; i <=m_nDM; i++) 

m_vsDM.push_back(m_lctrDisp.GetItemText(0, i)); 
//m_sTem = ; 
m_vsOption.push_back(m_lctrDisp.GetItemText(1, i)); 

CDialog::OnOK(); 

解决方案 »

  1.   

    你的代码我没有看,"内存只能READ"一般是调用了没有初始化的内存出现的
    “将枚举变量的位置稍微交换,编译连接没错”,这个应该不承在,只是你如果没有把//{{AFX_DATA(CSDMOption) 和//}}AFX_DATA 一起移动的话,ClassWizard将找不到你的定义,ClassWizard是根据这两个来判断的。
    如:你如果把DoDataExchange()函数里的//{{AFX_DATA_INIT(CSDMOption)和//}}AFX_DATA_INIT 删了,你再按Ctrl+W,在membervariables中将找不到该变量
      

  2.   

    内存不能为read是不是系统或编译器的问题啊?
      

  3.   

    内存不能为read通常情况下是使用了没有初始化的指针而引起,也就是常说的野指针了.