在同一个类内,如果想在函数 A() 内部调用 函数B();应该怎么弄?用函数指针么?还是怎么弄? 在同一个类内,如果想在函数 A() 内部调用 函数B();应该怎么弄?用函数指针么?还是怎么弄? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不行的,我在函数 A() 里面的一个if 判断语句里面调用时,它说那个被掉的函数未声明的,即使把被调函数设成全局函数还是不行! LZ贴代码比较好解决,应该B函数是静态的,所以无法调用 //视图类的CPP文件// rrView.cpp : implementation of the CRrView class//#include "stdafx.h"#include "rr.h"#include "rrDoc.h"#include "rrView.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endifint ab=0;/////////////////////////////////////////////////////////////////////////////// CRrViewIMPLEMENT_DYNCREATE(CRrView, CView)BEGIN_MESSAGE_MAP(CRrView, CView) //{{AFX_MSG_MAP(CRrView) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CRrView construction/destructionCRrView::CRrView(){ // TODO: add construction code here}CRrView::~CRrView(){}BOOL CRrView::PreCreateWindow(CREATESTRUCT& cs){ // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs);}/////////////////////////////////////////////////////////////////////////////// CRrView drawingvoid CRrView::OnDraw(CDC* pDC){ CRrDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here}/////////////////////////////////////////////////////////////////////////////// CRrView diagnostics#ifdef _DEBUGvoid CRrView::AssertValid() const{ CView::AssertValid();}void CRrView::Dump(CDumpContext& dc) const{ CView::Dump(dc);}CRrDoc* CRrView::GetDocument() // non-debug version is inline{ ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRrDoc))); return (CRrDoc*)m_pDocument;}#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////// CRrView message handlersvoid CRrView::Test(){ ab=99;}void CRrView::Trans(){ if(条件) { //想在这里调用函数 Test(); }}//视图类的头文件// rrView.h : interface of the CRrView class///////////////////////////////////////////////////////////////////////////////#if !defined(AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_)#define AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CRrView : public CView{protected: // create from serialization only CRrView(); DECLARE_DYNCREATE(CRrView)// Attributespublic: CRrDoc* GetDocument();// Operationspublic:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CRrView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: //}}AFX_VIRTUAL// Implementationpublic: void Trans(); static void Test(); virtual ~CRrView();#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected: //{{AFX_MSG(CRrView) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP()};#ifndef _DEBUG // debug version in rrView.cppinline CRrDoc* CRrView::GetDocument() { return (CRrDoc*)m_pDocument; }#endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_) 因为原工程代码太长了,就弄了个简单的例子,我要的功能是在函数 Trans()的if 判断语句里面调用函数 Test();Test() 声明为 static 类型 test函数为static应该可以通过函数名调用的。楼主试下吧。不成的话就找找其他地方是不是有问题,或者建个简单的工程试下。 正确,要用类名,因为static没有this指针.void CRrView::Trans(){ if(条件) { //想在这里调用函数 Test(); Test(); //这样写的话,实际上是: this->Test(),当然不行....解决办法,得闲没事干也别把类内函数随便声明为"static".. }} 不错,当声明为 被调函数为 static 的时候,在类内可以通过 类名::test()调用,也可以直接 用被调函数名来调用 : test(); 这两种方法都是可行的,谢谢楼上各位的帮忙! 为啥调试WINDOWS程序的时候不能在断点处停下? VC学习方向问题 listctrl 分页显示?高手来哈!给点您宝贵的意见! 属性页选项卡如果在左侧,文字如何实现横向显示? 组件注册失败..急 急急急!关于VC中MSChart控件的问题 寻找郑州兼职人员 画板的填充问题????大家帮帮忙!在线等待 怪异问题,紧急求助! 请帮助(急) MFC菜单问题 用windbg双机调试的问题
不行的,我在函数 A() 里面的一个if 判断语句里面调用时,它说那个被掉的函数未声明的,即使把被调函数设成全局函数还是不行!
// rrView.cpp : implementation of the CRrView class
//#include "stdafx.h"
#include "rr.h"#include "rrDoc.h"
#include "rrView.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endifint ab=0;
/////////////////////////////////////////////////////////////////////////////
// CRrViewIMPLEMENT_DYNCREATE(CRrView, CView)BEGIN_MESSAGE_MAP(CRrView, CView)
//{{AFX_MSG_MAP(CRrView)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CRrView construction/destructionCRrView::CRrView()
{
// TODO: add construction code here}CRrView::~CRrView()
{
}BOOL CRrView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs return CView::PreCreateWindow(cs);
}/////////////////////////////////////////////////////////////////////////////
// CRrView drawingvoid CRrView::OnDraw(CDC* pDC)
{
CRrDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}/////////////////////////////////////////////////////////////////////////////
// CRrView diagnostics#ifdef _DEBUG
void CRrView::AssertValid() const
{
CView::AssertValid();
}void CRrView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}CRrDoc* CRrView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRrDoc)));
return (CRrDoc*)m_pDocument;
}
#endif //_DEBUG/////////////////////////////////////////////////////////////////////////////
// CRrView message handlersvoid CRrView::Test()
{ ab=99;
}void CRrView::Trans()
{ if(条件)
{
//想在这里调用函数 Test();
}}
//视图类的头文件
// rrView.h : interface of the CRrView class
//
/////////////////////////////////////////////////////////////////////////////#if !defined(AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_)
#define AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CRrView : public CView
{
protected: // create from serialization only
CRrView();
DECLARE_DYNCREATE(CRrView)// Attributes
public:
CRrDoc* GetDocument();// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRrView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
//}}AFX_VIRTUAL// Implementation
public:
void Trans();
static void Test();
virtual ~CRrView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endifprotected:// Generated message map functions
protected:
//{{AFX_MSG(CRrView)
// NOTE - the ClassWizard will add and remove member functions here.
// DO NOT EDIT what you see in these blocks of generated code !
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};#ifndef _DEBUG // debug version in rrView.cpp
inline CRrDoc* CRrView::GetDocument()
{ return (CRrDoc*)m_pDocument; }
#endif///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_RRVIEW_H__EBA58E63_6159_404A_BB41_CB37E71FDDD6__INCLUDED_)
函数 Test();
Test() 声明为 static 类型
不成的话就找找其他地方是不是有问题,或者建个简单的工程试下。
{ if(条件)
{
//想在这里调用函数 Test();
Test(); //这样写的话,实际上是: this->Test(),当然不行....解决办法,得闲没事干也别把类内函数随便声明为"static"..
}}
也可以直接 用被调函数名来调用 : test();
这两种方法都是可行的,
谢谢楼上各位的帮忙!