在同一个类内,如果想在函数 A() 内部调用 函数B();应该怎么弄?用函数指针么?还是怎么弄? 

解决方案 »

  1.   


    不行的,我在函数 A() 里面的一个if 判断语句里面调用时,它说那个被掉的函数未声明的,即使把被调函数设成全局函数还是不行!
      

  2.   

    LZ贴代码比较好解决,应该B函数是静态的,所以无法调用
      

  3.   

    //视图类的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_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_)
      

  4.   

    因为原工程代码太长了,就弄了个简单的例子,我要的功能是在函数 Trans()的if 判断语句里面调用
    函数 Test();
    Test() 声明为 static 类型
      

  5.   

    test函数为static应该可以通过函数名调用的。楼主试下吧。
    不成的话就找找其他地方是不是有问题,或者建个简单的工程试下。
      

  6.   

    正确,要用类名,因为static没有this指针.void CRrView::Trans()
    {    if(条件)
        {
            //想在这里调用函数 Test();
            Test(); //这样写的话,实际上是: this->Test(),当然不行....解决办法,得闲没事干也别把类内函数随便声明为"static"..
        }}
      

  7.   

    不错,当声明为 被调函数为 static 的时候,在类内可以通过    类名::test()调用,
    也可以直接 用被调函数名来调用 :   test(); 
     这两种方法都是可行的,
    谢谢楼上各位的帮忙!