C++ <错误 C2059:语法错误:'constant'> 使用 const int 编译时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11782050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:31:59  来源:igfitidea点击:

<error C2059: syntax error : 'constant'> when compiling with const int

c++

提问by user1572019

I am getting the following errors when compiling the below code:

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

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 ';'


#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

回答by jxh

It is likely that NO_DATAis already defined as a macro elsewhere, and so it is expanding into something that does not agree with the compiler's notion of a variable name. Try re-naming NO_DATAto something else.

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

If there were no such conflict, the code as it were would compile fine, as demonstrated here.

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

回答by gilgamash

Even if this post has its age: The error can generally occur when multiple redefinitions, even regardless of upper/lower case, coexist. This includes potential preprocessor definitions in the solution's .vcprojxfile!. Consider something like

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

  <ItemDefinitionGroup>
    <ClCompile>
      <PreprocessorDefinitions>$(Configuration);%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>

in the above mentioned file. Now, having "Debug" and "Release" configurations you will most probably run into some problems and a potential source for the C2059 error. I experienced exaclty this dilemma.

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