Home  >  Q&A  >  body text

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

错误部分代码如下

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

提示错误位置在};这里

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

环境win10 ,Visual studio 2015 up1

PHPzPHPz2765 days ago663

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 13:33:32

    #include can only appear at the beginning of the file

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 13:33:32

    Suppose your header file is defined as FT_ERRORS_H.h, you need to modify it to

    static const struct ft_error ft_errors[] =
    {

    #include "FT_ERRORS_H.h"

    };

    That is, the header file needs to be included. When the compiler compiles, the contents in the header file will be mapped into the array.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 13:33:32

    FT_ERROR_H does not look like the name of a header file, maybe you are referring to "ft/error.h"?

    In addition, precompilation like #include can pass, and the text in the header file will be replaced here. But this
    is not conducive to debugging. You can talk about your needs and we can do it in another way.

    reply
    0
  • 黄舟

    黄舟2017-04-17 13:33:32

    The include directive must be defined in the file field (not only in the file header!), and must be defined before using any function package or variable defined in it for the first time.

    See ISO/IEC 9899:1999 standard section 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.

    In response to your problem, the #include directive should be removed from 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
    };
    

    Reference:

    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

    reply
    0
  • Cancelreply