visual-studio - C++小白,今天VS2015编译一个开源项目遇到如下问题
PHPz
PHPz 2017-04-17 13:31:32
0
4
610

错误部分代码如下

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

提示错误位置在};这里

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

环境win10 ,Visual studio 2015 up1

PHPz
PHPz

学习是最好的投资!

全部回覆(4)
黄舟

#include只能出現在檔案的開頭

Peter_Zhu

假設你的頭檔定義為 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 firany of the function to first be included或 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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!