简体   繁体   English

包括2个头文件时,类型重新定义错误

[英]type redefinition error when including 2 header files

I am compiling a Matlab mex file (Using VS2010 under Windows), and the following 2 includes: 我正在编译一个Matlab mex文件(在Windows下使用VS2010),以下2个文件包括:

#include <algorithm> // for std::copy
#include "mex.h"

give me compile error: 给我编译错误:

1>d:\\svn\\trunk\\dev\\matlab\\extern\\include\\matrix.h(337): error C2371: 'char16_t' : redefinition; 1> d:\\ svn \\ trunk \\ dev \\ matlab \\ extern \\ include \\ matrix.h(337):错误C2371:'char16_t':重新定义; different basic types 不同的基本类型

I have tried putting it in a namespace: 我尝试将其放在命名空间中:

namespace Algo {    
    #include <algorithm>
}

But then I get tons of other compile errors, without even using anything defined in <algorithm> , for example: 但是然后我得到了很多其他的编译错误,甚至没有使用<algorithm>定义的任何内容,例如:

Error   1   error C2039: 'set_terminate' : is not a member of '`global namespace''  C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception    192
Error   2   error C2873: 'set_terminate' : symbol cannot be used in a using-declaration C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception    192

How can I solve this? 我该如何解决?

Putting an standard header in a namespace doesn't sound like a good idea, even though you are not using any of the methods or classes there, there is a pretty big chance that another header might (like the mex.h). 尽管您没有在其中使用任何方法或类,但在名称空间中放置标准标头听起来并不是一个好主意,但另一个标头(例如mex.h)的可能性很大。 Putting the namespace around the mex header seems much less probable to create a conflict. 将名称空间放在mex标头周围似乎不太可能产生冲突。

Also including one header before the other might also prevent such a conflict. 在另一个头之前还包含一个头也可以防止这种冲突。 Some headers take into account that some symbol might already be defined before redefining them (library developer really should take care to do this) 有些标头考虑到在重新定义它们之前可能已经定义了一些符号(库开发人员确实应该注意这一点)

In some cases reading the headers might also give you a good clue what is going on. 在某些情况下,阅读标题可能也为您提供了一个很好的线索,说明发生了什么。 Some times it might be as simple as defining a symbol, which tells the header to skip the redefinition. 有时,定义一个符号可能很简单,它告诉标题跳过重定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM