Visual C++调试断言失败

Visual C++ Debug Assertion Fail

本文关键字:失败 断言 调试 C++ Visual      更新时间:2023-10-16

当我编译程序时,它会说"调试断言失败"
当我点击忽略两次时,我的应用程序就会出现
我该怎么修?

下面是整个代码,以防您想对其进行自我测试。该应用程序的数量随着滚动条向右移动而增加,随着滚动条向左移动而减少。但我的主要问题是调试断言失败了。如果你能发布代码中有问题的部分,以及我如何修复它或修复它并发布代码。有什么方法可以通过使用try-and-catch来消除运行时的错误吗。我的意思是请解决那个问题。这是一个项目,我只有一周半的时间来做。如果你能帮我做这个项目,你的帮助将不胜感激。我对visual c++还相当陌生。部分错误是:

程序E:\BHCCHardwareStore\Debug\bHCHardwareStore.exe FIle:f\dd\vctools\vc7libs\ship\atmfc\src\mfc\appcore.cpp行:196。有关程序如何导致断言失败的信息,请参阅有关断言的Visual C++文档。(按"重试"可调试应用程序)。

现在,当我点击忽略它时,它会显示应用程序非常好。这个应用程序似乎运行得很好。如何修复此错误此代码不是我剪切和粘贴的全部代码?

#include "stdafx.h"
#include <strstream>
#include <afxwin.h>
#include <string.h>
const int IDC_SB1 = 100;
const int IDC_CS1 = 101;
const int IDC_CS2 = 102;
const int IDC_BUTTON = 103;
const int MIN_RANGE = 0;
const int MAX_RANGE = 100;
class CApp :public CWinApp
{
public:
    virtual BOOL InitInstance();
};
CApp App;
class CSource :public CFrameWnd
{
    CScrollBar*sb1;
    CStatic* cs1;
    CStatic* cs2;
    CButton* button;
public:
    CSource();
    afx_msg void OnHScroll(UINT nSBCode,
        UINT nPos, CScrollBar* pSccrollBar);
    afx_msg void handleButton();
    DECLARE_MESSAGE_MAP();
};
BEGIN_MESSAGE_MAP(CSource, CFrameWnd)
    ON_WM_HSCROLL()
    ON_BN_CLICKED(IDC_BUTTON, handleButton)
END_MESSAGE_MAP()
//Window cONSTRUCTOR
CSource::CSource()
{
}
void CSource::OnHScroll(UINT nSBCode,
    UINT nPos, CScrollBar* pScrollBar)
{
    int pos, dividend = 0, holder = 0, x = 0;
    char array[9];
    //;
    pos = pScrollBar->GetScrollPos();
    switch (nSBCode)
    {
    case SB_LINEUP:
        pos -= 1;
        break;
    case SB_LINEDOWN:
        pos += 1;
        break;
    case SB_PAGEUP:
        pos -= 10;
        break;
    case SB_PAGEDOWN:
        pos += 10;
        break;
    case SB_TOP:
        pos = MIN_RANGE;
        break;
    case SB_BOTTOM:
        pos = MAX_RANGE;
        break;
    case SB_THUMBTRACK:
        pos = nPos;
        break;
    default:
        return;
    }
    if (pos < MIN_RANGE)
        pos = MIN_RANGE;
    else if (pos > MAX_RANGE)
        pos = MAX_RANGE;
    sb1->SetScrollPos(pos, TRUE);
    //Set the labels to the new values
    char s[100];
    TCHAR s1[100];
    std::ostrstream ostr(s, 100);
    ostr << "Decimal Value = " << pos << std::ends;
    for (int i = 0; i < 100; i++){
        s1[i] = (TCHAR) s[i];
    }
    SetDlgItemText(IDC_CS1, s1);
    ostr.seekp(std::ios::beg);
    dividend = pos;
    for (int y = 0; y < 9; y++)
        array[y] = '0';
    int remainder = dividend % 2;
    int quotient = dividend / 2;
    array[x] = (char)(remainder + 48);
    do
    {
        remainder = quotient % 2;
        quotient = quotient / 2;
        array[++x] = (char)(remainder + 48);
    } while (quotient != 0);
    array[8] = '';
    ostr << "Binary Value = " << _strrev(array) << std::ends;
    SetDlgItemText(IDC_CS2, s1);
}
void CSource::handleButton()
{
    int result;
    result = MessageBox(_T("Are you sure?"), _T("Exiting"),
        MB_ICONQUESTION | MB_YESNO);
    if (result == IDYES)
    {
        Beep(1000, 100);
        DestroyWindow();
    }
    else
        Beep(200, 100);
}
//Initialize the application and the main window
BOOL CApp::InitInstance()
{
    m_pMainWnd = new CSource();
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

您的代码(在VS2013中)在MFC代码中生成断言:

BOOL CWnd::ShowWindow(int nCmdShow)
{
    ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

来自您的电话:

m_pMainWnd->ShowWindow(m_nCmdShow);

您必须先创建窗口,然后才能显示它。