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

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

错误部分代码如下

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

提示错误位置在};这里

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

环境win10 ,Visual studio 2015 up1


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


#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,你可以说说你的需求,我们可以换一种方式来做这个事情。

【热门文章】
【热门文章】