首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

在一个类中修改另一个类的成员变量出现异常,不知咋回事?(急)

2012-01-14 
在一个类中修改另一个类的成员变量出现错误,不知怎么回事?(急)主要代码:voidCMyTestDlg::OnButton1(){//TO

在一个类中修改另一个类的成员变量出现错误,不知怎么回事?(急)
主要代码:
void   CMyTestDlg::OnButton1()  
{
//   TODO:   Add   your   control   notification   handler   code   here

pCal-> m_pa.x=100;
pCal-> m_pa.y=12;
pCal-> SetNumber();
CString   str;
str.Format( "%d ",xx);
MessageBox(str);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#if   !defined(AFX_MYCAL_H__170633DB_B3D1_4467_A3E1_08295444DADC__INCLUDED_)
#define   AFX_MYCAL_H__170633DB_B3D1_4467_A3E1_08295444DADC__INCLUDED_

#if   _MSC_VER   >   1000
#pragma   once
#endif   //   _MSC_VER   >   1000

struct   Param
{
int   x;
int   y;
};
class   MyCal     //自定义类
{
public:
Param   m_pa;
void   SetNumber();
MyCal();
virtual   ~MyCal();

};

#endif


#include   "stdafx.h "
#include   "MyTest.h "
#include   "MyCal.h "

#ifdef   _DEBUG
#undef   THIS_FILE
static   char   THIS_FILE[]=__FILE__;
#define   new   DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
//   Construction/Destruction
//////////////////////////////////////////////////////////////////////
int   xx;
MyCal::MyCal()
{

}

MyCal::~MyCal()
{

}

void   MyCal::SetNumber()
{
xx=m_pa.x*m_pa.y;
}


[解决办法]
pCal有效吗?换成下面试试
void CMyTestDlg::OnButton1()
{
MyCal cal;
cal.m_pa.x=100;
cal.m_pa.y=12;
cal.SetNumber();
CString str;
str.Format( "%d ",xx);
MessageBox(str);
}
[解决办法]
建议楼主修改一个你那个类的成员函数:
void MyCal::SetNumber(CMyTestDlg *pwnd)
{
  pwnd-> xx=m_pa.x*m_pa.y;
      //不明白楼主那个用法编译没有错误吗?可以直接用吗? xx=m_pa.x*m_pa.y; ??
}

//调用时这样.
pCal-> SetNumber(this);

热点排行