Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ VS2013-同一标头的多个包含时出错_C++_Visual C++_Visual Studio 2013_C Preprocessor - Fatal编程技术网

C++ VS2013-同一标头的多个包含时出错

C++ VS2013-同一标头的多个包含时出错,c++,visual-c++,visual-studio-2013,c-preprocessor,C++,Visual C++,Visual Studio 2013,C Preprocessor,在将一个项目从VisualStudio2005移植到2013年时,我遇到了这种奇怪的行为,对此我找不到解释。上下文是关于通过多次包含某个头文件来创建模板专门化,但在每次包含之前更改预处理器定义,以基本上生成不同的类声明 我可以将问题缩小到以下情况: gen.hpp #ifdef ENABLE_GEN #ifdef GEN_SWAP_ORDER // (1) class Foo {}; #else class Bar {}; #endif #endif #define ENABL

在将一个项目从VisualStudio2005移植到2013年时,我遇到了这种奇怪的行为,对此我找不到解释。上下文是关于通过多次包含某个头文件来创建模板专门化,但在每次包含之前更改预处理器定义,以基本上生成不同的类声明

我可以将问题缩小到以下情况:

gen.hpp

#ifdef ENABLE_GEN

#ifdef GEN_SWAP_ORDER // (1)
   class Foo {};
#else
   class Bar {};
#endif

#endif
#define ENABLE_GEN

#include "gen.hpp"
#define GEN_SWAP_ORDER
#include "gen.hpp"

int main()
{
   Foo foo;
   Bar bar;
}
main.cpp

#ifdef ENABLE_GEN

#ifdef GEN_SWAP_ORDER // (1)
   class Foo {};
#else
   class Bar {};
#endif

#endif
#define ENABLE_GEN

#include "gen.hpp"
#define GEN_SWAP_ORDER
#include "gen.hpp"

int main()
{
   Foo foo;
   Bar bar;
}
这与预期一样有效,即
Foo
Bar
都在
main()
中声明并可用

现在,要引起该问题,请将(1)标记行中的
#ifdef
更改为
#ifndef
,这只会导致声明交换
Foo
Bar
的顺序。但是,编译失败了:

1>c:\path\to\main.cpp(10): error C2065: 'Bar' : undeclared identifier
1>c:\path\to\main.cpp(10): error C2146: syntax error : missing ';' before identifier 'bar'
1>c:\path\to\main.cpp(10): error C2065: 'bar' : undeclared identifier
预处理的文件如下所示(去掉一些空白):

我的问题是:我遗漏了什么吗?这种预期行为是出于某种原因吗?是否是编译器设置/错误导致Visual Studio在认为它具有标头保护时(由于
#ifndef
)第二次跳过标头内容(包括
#else
部分)


谢谢

这是根据dyp的评论,并在VS2013 RTM中修复。

即使在执行完全清理+重建(以排除预编译的标题骗局)时也会发生这种情况吗?Visual Studio包含对包含防护的优化,可能它们有问题?有一个发布前的bug看起来很相似:Zinger,他们没有完全修复这个bug。可见//SeaFipe,您将看到该文件只包含一次。我不能用Visual C++ 2013 RTM(18.00 .21005.01)或更新3(18.00。30723.00)来重新处理。我误读了您的问题,没有注意到我需要将
#ifdef
更改为
#ifndef
来重新设置问题。我重新激活了连接错误并通知了我们的编译器团队。谢谢你让我们注意到这一点。