在一个类中修改另一个类的成员变量出现错误,不知怎么回事?(急)
主要代码:
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);