在While循环中添加break语句时,为什么会出现错误?

问题描述

这是我使用vcvarsall(Microsoft Visual Studio的工具)运行的代码的一部分。

   Running = true;
    while (Running);
    {
        MSG Message;
        BOOL MessageResult = GetMessageA(&Message,0);
        if (MessageResult > 0)
        {
            TranslateMessage(&Message);
            dispatchMessage(&Message);
        }
        else
        {
            break;
        }
    }

我从命令行收到此错误

错误C2043:非法中断

我做了一些研究,并从MSDN中找到了

 A break is legal only within a do,for,while,or switch statement.

链接https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2043?view=vs-2019

那么我该如何解决错误?从我发现的其他C ++文档和示例中,这是合法的。这是特定于Visual Studio中运行的C ++代码的问题吗?谢谢!

解决方法

原来是语法错误!不要在循环后添加;!此外,只要在循环内,只要添加switch语句,就可以添加break语句。