问题描述
我正在 OpenSees(一个主要用 Visual Studio C++ 编写的开源地震工程模拟项目)编写自适应步长更新算法.我在两个不同的头文件(即 windef.h 和 steelz01.h)中面临两个同名变量之间的冲突.我需要一种方法来解决这个冲突.
I am writing an adaptive step size update algorithm in OpenSees (an opensource earthquake engineering simulation project written majorly in visual studio c++). I am facing a conflict between two variables having the same name in two different header files (namely, windef.h and steelz01.h). I need a way to resolve this conflict.
我在我的项目中使用了gnuplot-iostream.h,只有当我包含这个头文件时我才会遇到这个冲突,否则没有冲突,代码构建完美.
I am using gnuplot-iostream.h in my project, I am facing this conflict only when I include this header file, otherwise there is no conlfict, code is builidng perfectly.
基本上gnuplot-iostream.h 正在调用windows.h,后者进一步调用windef.h.我在 steelz01.h 文件中添加了 include gauards,但它没有解决问题.
Basically gnuplot-iostream.h is calling windows.h, which is further calling windef.h. I have added include gauards in steelz01.h file, but it did not resolve the issue.
当我将 steelz01.h 中的 varaibale 名称更改为不同的名称时,代码也可以完美构建.没有发现问题.但是,我不想在 steelz01 中更改变量的名称,它会产生严重的影响.
When I change the varaibale name in steelz01.h to a different name, then also the code is perfectly building. No ISSUE found. But, I don't want to channge the name of the variable in steelz01, it has serious repercussions.
我包含这样的头文件
#include "gnuplot-iostream.h"
#include <SteelZ01.h>
这是在 steelz01 中定义变量 SIZE 的方式
This is how the variable SIZE is defined in steelz01
#define LOOP_NUM_LIMIT 30
const int SIZE = LOOP_NUM_LIMIT; //limit of array number
在windef.h中是这样定义的
and in windef.h, it is defined like this
typedef struct tagSIZE
{
LONG cx;
LONG cy;
} SIZE, *PSIZE, *LPSIZE;
typedef SIZE SIZEL;
typedef SIZE *PSIZEL, *LPSIZEL;
Visual Studio 2017 抛出此错误,
Visual Studio 2017 is throwing this error,
1>c:\program files (x86)\windows kits\8.1\include\shared\windef.h(190): error C2378: 'SIZE': redefinition; symbol cannot be overloaded with a typedef
1>e:\phd working folder\0_ops_github\src\material\nd\reinforcedconcreteplanestress\steelz01.h(17): note: see declaration of 'SIZE'
我期待有一种方法可以解决此冲突并成功构建.
I am expecting a way to resolve this conflict and a successful build.
推荐答案
我建议你将 include 语句放在命名空间中,
I would suggest you to put include statement in namespace,
namespace ABC
{
#include "gnuplot-iostream.h"
}
namespace PQR
{
#include <SteelZ01.h>
}
调用:
ABC::SIZE
PQR::SIZE
这不会改变现有库的任何代码.但是,使用通用名称的库的作者因此建议他将通用名称保留在命名空间下以减少任何冲突.
This will not change any code of existing libraries. However, author of library using common names hence suggest him to keep common name under namespace to reduce any conflict.
这篇关于如何解决头文件中的变量冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!