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

Ale*_*xey 0 c++ compiler-errors include arrayfire

我有一个程序依赖于多个包含文件。当我按照下面所示的顺序定义包含时,程序可以正常编译。

#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"
Run Code Online (Sandbox Code Playgroud)

但是,当我切换最后两个包含时,如下所示

#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
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

1>d:\libraries\tbb41\tbb41_20130613oss\include\tbb\task.h(765): 错误 C2059: 语法错误: '{' 1>d:\libraries\tbb41\tbb41_20130613oss\include\tbb\task.h( 765): 错误 C2334: '{' 之前的意外标记;跳过明显的函数体

这是出乎意料的,我想解决它。所有包含内容均来自库(OpenCV 和 ArrayFire)。关于可能的原因以及如何解决此问题有什么建议吗?

编辑这是task.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 */
Run Code Online (Sandbox Code Playgroud)

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

Mik*_*our 5

这是由ArrayFire 标头之一中的以下问题引起的:

#define group(...)  __VA_ARGS__
Run Code Online (Sandbox Code Playgroud)

这定义了一个类似函数的宏,它被宏参数列表替换;group(a,b)展开为a,b,并且(这里更重要的是)group()展开为空。由于宏不尊重诸如作用域之类的语言级概念,这会干扰后面的声明:

task_group_context* group () { return prefix().context; }
Run Code Online (Sandbox Code Playgroud)

将其转换为

task_group_context* { return prefix().context; }
Run Code Online (Sandbox Code Playgroud)

这不是有效的声明。

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