由于头文件中的语法错误,导致编译错误

Compile error due to syntax error in header file

本文关键字:错误 编译 语法 于头 文件      更新时间:2023-10-16

我有一个依赖于几个include文件的程序。当我按照下面显示的顺序定义includes时,程序编译得很好。

#include <iostream>
#include "opencv2/cvconfig.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/internal.hpp" // For TBB wrappers
#include "arrayfire.h"

然而,当我切换最后两个时,包括如下所示

#include <iostream>
#include "opencv2/cvconfig.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "arrayfire.h"
#include "opencv2/core/internal.hpp" // For TBB wrappers

我得到编译器错误:

1> d:\libraries\tbb41\tbb41_20130613oss\include\tbb\task.h(765):错误C2059:语法错误:"{"1> d:\libraries\tbb41\tbb41_20130613oss\include\tbb\task.h(765):错误C2334:"{"之前的意外令牌;正在跳过明显的函数机身

这是出乎意料的,我想修复它。所有的include都来自库(OpenCV和ArrayFire)。关于可能的原因以及如何解决这个问题,有什么建议吗?

编辑这是任务的相关部分。h

759 #if __TBB_TASK_GROUP_CONTEXT
760    //! This method is deprecated and will be removed in the future.
761    /** Use method group() instead. **/
762    task_group_context* context() {return prefix().context;}
763
764    //! Pointer to the task group descriptor.
765    task_group_context* group () { return prefix().context; }
766 #endif /* __TBB_TASK_GROUP_CONTEXT */

在第765行中,IDE抱怨{,称Error: expected an identifier

这是由其中一个ArrayFire标头中的以下邪恶引起的:

#define group(...)  __VA_ARGS__

这定义了一个类似函数的宏,该宏被宏参数列表所取代;group(a,b)扩展到a,b,(这里更重要的是)group()扩展到零。由于宏不尊重范围等语言级别的概念,这会干扰后面的声明:

task_group_context* group () { return prefix().context; }

将其转换为

task_group_context* { return prefix().context; }

这不是一个有效的声明。

修复方法是最后包含"arrayfire.h",并注意在自己的代码中尝试使用哪些名称;或者在包括它之后对#undef group(以及它可能犯下的任何其他邪恶)。或者,如果可能的话,用火杀死它,用一些不那么邪恶的东西代替。