2014-05-04 27 views
0

我在測試2.cpp此功能C++ MFC:嘗試使用一個類的實例調用方法時調試斷言錯誤

void Ctest2App::message(){ 
    MessageBox(0, L"And text here", L"MessageBox caption", MB_OK); 

} 

我以下列方式

void Ctest2Dlg::OnBnClickedButton1() 
{ 
    Ctest2App t; 
    t.message(); 
} 

把它從test2Dlg當我按下按鈕時,出現Debug Assertion錯誤。爲什麼?

的測試2.cpp文件 -

// test2.cpp : Defines the class behaviors for the application. 
// 

#include "stdafx.h" 
#include "test2.h" 
#include "test2Dlg.h" 

#ifdef _DEBUG 
#define new DEBUG_NEW 
#endif 


// Ctest2App 

BEGIN_MESSAGE_MAP(Ctest2App, CWinApp) 
    ON_COMMAND(ID_HELP, &CWinApp::OnHelp) 
END_MESSAGE_MAP() 


// Ctest2App construction 

Ctest2App::Ctest2App() 
{ 
    // TODO: add construction code here, 
    // Place all significant initialization in InitInstance 
} 


// The one and only Ctest2App object 

Ctest2App theApp; 


// Ctest2App initialization 

BOOL Ctest2App::InitInstance() 
{ 
    // InitCommonControlsEx() is required on Windows XP if an application 
    // manifest specifies use of ComCtl32.dll version 6 or later to enable 
    // visual styles. Otherwise, any window creation will fail. 
    INITCOMMONCONTROLSEX InitCtrls; 
    InitCtrls.dwSize = sizeof(InitCtrls); 
    // Set this to include all the common control classes you want to use 
    // in your application. 
    InitCtrls.dwICC = ICC_WIN95_CLASSES; 
    InitCommonControlsEx(&InitCtrls); 

    CWinApp::InitInstance(); 


    AfxEnableControlContainer(); 

    // Create the shell manager, in case the dialog contains 
    // any shell tree view or shell list view controls. 
    CShellManager *pShellManager = new CShellManager; 

    // Standard initialization 
    // If you are not using these features and wish to reduce the size 
    // of your final executable, you should remove from the following 
    // the specific initialization routines you do not need 
    // Change the registry key under which our settings are stored 
    // TODO: You should modify this string to be something appropriate 
    // such as the name of your company or organization 
    SetRegistryKey(_T("Local AppWizard-Generated Applications")); 

    Ctest2Dlg dlg; 
    m_pMainWnd = &dlg; 
    INT_PTR nResponse = dlg.DoModal(); 
    if (nResponse == IDOK) 
    { 
     // TODO: Place code here to handle when the dialog is 
     // dismissed with OK 
    } 
    else if (nResponse == IDCANCEL) 
    { 
     // TODO: Place code here to handle when the dialog is 
     // dismissed with Cancel 
    } 

    // Delete the shell manager created above. 
    if (pShellManager != NULL) 
    { 
     delete pShellManager; 
    } 

    // Since the dialog has been closed, return FALSE so that we exit the 
    // application, rather than start the application's message pump. 
    return FALSE; 
} 

void Ctest2App::message(){ 
    MessageBox(0, L"And text here", L"MessageBox caption", MB_OK); 

} 
+0

它肯定有事情做與'Ctest2App',因爲調用'MessageBox'似乎確定。什麼是'Ctest2App'? – alain

+0

它是一個新創建的基於對話框的MFC項目。除了構造函數,對象和InitInstance之外,沒有什麼。 – user2557850

回答

1

您試圖創建第二個Ctest2App對象,但只能有一個在一個應用程序。 MFC提供了一個全局函數訪問已經創建的應用程序對象:

AfxGetApp()->message(); 
相關問題