我有一个依赖于多个包含文件的程序。当我按照下面显示的顺序定义包含时,程序可以正常编译。
#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): error C2059: syntax error : '{' 1>d:\libraries\tbb41\tbb41_20130613oss\include\tbb\task.h(765): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
这是意外的,我想修复它。所有包含都来自库(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 */
在第 765 行,IDE 提示 {
,说 Error: expected an identifier
最佳答案
这是由 ArrayFire headers 之一中的以下邪恶造成的:
#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
(以及它可能犯下的任何其他罪恶)在包含它之后。或者,如果可能的话,用火杀死它并使用不那么邪恶的东西。
关于c++ - 由于头文件中的语法错误导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620621/