C++小白,今天VS2015编译一个开源项目遇到如下问题

新手上路,请多包涵

clipboard.png

错误部分代码如下

static const struct ft_error ft_errors[] =
{
#include FT_ERRORS_H
};

提示错误位置在};这里

求问这是什么错误?导致的原因是什么?如何解决?

环境win10 ,Visual studio 2015 up1

阅读 6.8k
4 个回答

#include只能出现在文件的开头

假设你的头文件定义为 FT_ERRORS_H.h,则需要修改为

static const struct ft_error ft_errors[] =
{

#include "FT_ERRORS_H.h"

};

即需要包含头文件。在编译器编译的时候,会把头文件里的内容映射到数组内。

FT_ERROR_H看起来不像是一个头文件的名字,或许你指的是 "ft/error.h" ?

另外,#include这么用预编译是能通过的,头文件里的文本会被替换到这里来。但是这样
不利于debug,你可以说说你的需求,我们可以换一种方式来做这个事情。

include指令必须定义于文件域(不是只能定义在文件头!),同时必须在第一次使用其中定义的任何函数包或者变量前定义。

参见ISO/IEC 9899:1999标准7.1.2章节:
If used, a header shall be included outside of any external declaration or definition, and it shall first be included before the first reference to any of the functions or objects it declares, or to any of the types or macros it defines.

针对你的问题,应该把#include指令从ft_errors中提出来:

#include FT_ERRORS_H
// I assume that FT_ERRORS_H has been correctly defined with corresponding header file name
static const struct ft_error ft_errors[] =
{
    // concrete struct definition/declaration
};

参考:

  1. http://stackoverflow.com/questions/16389126/can-the-pre-processor-directives-like-include-be-placed-only-at-the-top-of-the

  2. http://www.dii.uchile.cl/~daespino/files/Iso_C_1999_definition.pdf

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题