简体   繁体   English

错误C2059:语法错误:'常量'

[英]error C2059: syntax error : 'constant'

I've got a piece of code that's automatically generated that compiles on Linux but not on Windows using Visual Studio 2008 Express. 我有一段代码是自动生成的,可以在Linux上编译,但不能在Windows上使用Visual Studio 2008 Express编译。 The issue I'm having is that I don't understand the compiler error. 我遇到的问题是我不理解编译器错误。 I don't think I can post the exact code, so here's a sanitized version of it... 我不认为我可以发布确切的代码,所以这是一个消毒版本...

Error is reported for the line declaring the static const DELETE. 报告静态const DELETE的行报告错误。 Note: The compiler error doesn't show up when this file is compiled - it builds into a library successfully, but shows up in a second project that includes the header (indirectly). 注意:编译此文件时编译器错误不会显示 - 它会成功构建到库中,但会显示在包含标题(间接)的第二个项目中。 I believe there are at least one or two other projects that include it indirectly in the solution - they have no issues compiling. 我相信至少有一两个其他项目在解决方案中间接包含它 - 它们没有任何问题编译。

File_A.h:

enum LONG_TYPE_NAME {
  ENUM_NAME_PREFIX_ADD = 0,
  ENUM_NAME_PREFIX_CHANGE = 1,
  ENUM_NAME_PREFIX_DELETE = 2,
  ENUM_NAME_PREFIX_SOMETHINGELSE = 3,
};
//Lots of code here
class FOO : public ::LIBRARY_NAME {
 public:
  //Some stuff
  private:
  //Some stuff
  public:
  //Some more stuff

  typedef LONG_TYPE_NAME SHORT_NAME;
  static const SHORT_NAME ADD = ENUM_NAME_PREFIX_ADD;
  static const SHORT_NAME CHANGE = ENUM_NAME_PREFIX_CHANGE; 

  /* compiler error for the following line only*/
  static const SHORT_NAME DELETE = ENUM_NAME_PREFIX_DELETE; 
  static const SHORT_NAME SOMETHINGELSE = ENUM_NAME_PREFIX_SOMETHINGELSE; 

  //More stuff
};

The constant itself only shows up in one place (when I search through the project for the term DELETE): 常量本身只显示在一个地方(当我在项目中搜索术语DELETE时):

File_A.cc:

#ifndef _MSC_VER
const LONG_TYPE_NAME FOO::ADD;
const LONG_TYPE_NAME FOO::CHANGE;
const LONG_TYPE_NAME FOO::DELETE;
//More stuff
#endif  // _MSC_VER

The error reported is error C2059: syntax error : 'constant' (followed by error C2258: illegal pure syntax, must be '= 0' and error C4430: missing type specifier - int assumed. Note: C++ does not support default-int which I assume are not relevant), but not when the files above are being compiled. 报告的错误是error C2059: syntax error : 'constant' (后跟error C2258: illegal pure syntax, must be '= 0'并且error C4430: missing type specifier - int assumed. Note: C++ does not support default-int我假设不相关),但不是在编译上面的文件时。

The files are compiled to a library which is statically linked to by another project (C++) - this is the one that's generating the error (as well as in a second .cpp file that does something similar). 这些文件被编译成一个库,该库通过另一个项目(C ++)静态链接 - 这是生成错误的文件(以及执行类似操作的第二个.cpp文件)。 It still shows up when I comment out all the code, so I assume it has something to do with header inclusions. 当我注释掉所有代码时它仍会出现,所以我认为它与标题包含有关。

Commenting out the line generating the error makes the build work on Windows (and fail on Linux, but I assume that commenting out its counterpart in the ifndef should fix that), but I really want to know why the compiler is failing for that particular line and what the error actually means. 注释掉生成错误的行会使构建工作在Windows上(并且在Linux上失败,但我认为在ifndef中注释掉它的对应部分应该修复它),但我真的想知道编译器为什么会失败的特定行以及错误实际意味着什么。 Also, it's probably better not to modify code that's been automatically generated. 此外,最好不要修改自动生成的代码。

EDIT: Splitting up the terms into individual lines makes the compiler point to the DELETE line. 编辑:将术语拆分为单独的行使编译器指向DELETE行。 Maybe there's a macro automatically defined with the name DELETE somewhere? 也许有一个自动定义名称DELETE的宏?

EDIT 2: Cleaned up the heading section a bit to clear up some possible misconceptions. 编辑2:稍微清理标题部分以清除一些可能的误解。 Incidentally, renaming the DELETE variable also clears out the error. 顺便说一句,重命名DELETE变量也会清除错误。

EDIT 3: Clearly I need to learn more about VS - /P generates the preprocessed file without producing the object file, so the build will of course fail without generating compilation errors. 编辑3:显然我需要了解更多关于VS - / P生成预处理文件而不生成目标文件,因此构建当然会失败而不会产生编译错误。 Also, it does look like there is a macro somewhere, which defines DELETE as (0x00010000L). 此外,它确实看起来像某处有一个宏,它将DELETE定义为(0x00010000L)。

肯定会在某处定义宏DELETE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM