<error C2059:语法错误:'constant'>使用const int进行编译时

use*_*019 11 c++

编译以下代码时出现以下错误:

3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2059: syntax error : 'constant'
3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2238: unexpected token(s) preceding ';'
Run Code Online (Sandbox Code Playgroud)
#if !defined(AisTarget_h)
#define AisTarget_h

#include "GeneralAviationItems.h"
#include <string>

namespace HEDGE {
    using namespace GeneralAviation; 

    class AisTarget : public WaypointLatLon {
        public:
            static const int NO_DATA = -1000; //here is the error
    };    
} // end namespace HEDGE

#endif
Run Code Online (Sandbox Code Playgroud)

jxh*_*jxh 26

很可能NO_DATA已经将其定义为其他地方的宏,因此它正在扩展为与编译器的变量名称概念不一致的东西.尝试重新命名NO_DATA为其他内容.

如果没有这种冲突,因为它是代码将编译罚款,这表现在这里.

  • 这就是为什么我不在 C++ 中使用大写标识符。标准库中有太多鲁莽命名的宏。windows.h 太可怕了 (2认同)
  • 来到这里是因为 NO_ERROR,感谢 winerror.h。NO_DATA 是在 WinSock2.h 中定义的 (2认同)

gil*_*ash 5

即使这篇文章有它的年龄:当多个重新定义(即使大小写)共存时,通常也会发生错误。这包括解决方案的.vcprojx文件中潜在的预处理器定义!. 考虑类似

  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions>$(Configuration);%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>
Run Code Online (Sandbox Code Playgroud)

在上面提到的文件中。现在,有了“调试”和“发布”配置,您很可能会遇到一些问题和 C2059 错误的潜在来源。我经历过这种困境。