如何解决头文件中的变量冲突?
How to resolve variable conflicts in header files?
我正在OpenSees(一个主要用Visual Studio c ++编写的开源地震工程模拟项目)中编写自适应步长更新算法。我在两个不同的头文件(即windef.h和steelz01.h)中面临两个具有相同名称的变量之间的冲突。我需要一种方法来解决这个冲突。
我在我的项目中使用gnuplot-iostream.h,只有当我包含这个头文件时,我才会面临这种冲突,否则就没有联系,代码是完美的
。基本上gnuplot-iostream.h调用的是 windows.h,后者进一步调用windef.h。我已经在 steelz01.h 文件中添加了包含 gauards,但它没有解决问题。
当我将 steelz01.h 中的 varaibale 名称更改为其他名称时,代码也完美构建。未发现问题。但是,我不想在 steelz01 中修改变量的名称,它会产生严重影响。
我包含这样的头文件
#include "gnuplot-iostream.h"
#include <SteelZ01.h>
这就是变量 SIZE 在 steelz01 中的定义
方式#define LOOP_NUM_LIMIT 30
const int SIZE = LOOP_NUM_LIMIT; //limit of array number
在 Windef.h 中,它是这样定义的
typedef struct tagSIZE
{
LONG cx;
LONG cy;
} SIZE, *PSIZE, *LPSIZE;
typedef SIZE SIZEL;
typedef SIZE *PSIZEL, *LPSIZEL;
Visual Studio 2017 抛出此错误,
1>c:program files (x86)windows kits8.1includesharedwindef.h(190): error C2378: 'SIZE': redefinition; symbol cannot be overloaded with a typedef
1>e:phd working folder _ops_githubsrcmaterialndreinforcedconcreteplanestresssteelz01.h(17): note: see declaration of 'SIZE'
我期待一种解决此冲突的方法并成功构建。
我建议你将包含语句放在命名空间中,
namespace ABC
{
#include "gnuplot-iostream.h"
}
namespace PQR
{
#include <SteelZ01.h>
}
叫:
ABC::SIZE
PQR::SIZE
这不会更改现有库的任何代码。但是,使用通用名称的库作者因此建议他将通用名称保留在命名空间下以减少任何冲突。
相关文章:
- 尝试通过共享指针使用变量时读取访问冲突
- 局部变量名称冲突
- 如何处理冲突的函数和变量名称?
- 在链接的程序集文件中,我想从 c++ 调用代码访问变量.是否可以在不触发访问冲突的情况下执行此操作?
- 模块名称是否驻留在单独的"name space"中,或者它们可能与例如变量名称发生冲突?
- 如何解决头文件中的变量冲突?
- c++11 lambda 真的支持闭包吗?函数变量中存在语义冲突
- 尝试使用成员函数访问动态分配的成员变量时读取访问冲突
- 相互冲突名称的成员变量和方法变量
- 相同定义变量的部分类型冲突
- 写入结构变量时发生strcpy访问冲突
- C++-引发异常:读取访问冲突.变量为nullptr
- int max(一个变量)和 int max(int ,int)之间没有冲突,这是 C++ 中的一个函数
- 当函数在fread()应用于局部结构变量后返回时发生访问冲突
- C++ 当命名空间发生冲突时,为什么结构/类不与变量和函数发生冲突?
- 声明变量时发生访问冲突
- 变量和 getter 的 Qt 命名约定,这样它们就不会发生冲突?
- OpenGL 4.3程序Ubuntu 14.04 x64中的时间变量冲突问题
- 传递变量时的访问冲突
- 可以净化找出栈变量上的访问冲突