如何解决头文件中的变量冲突?

How to resolve variable conflicts in header files?

本文关键字:变量 冲突 文件 何解决 解决      更新时间:2023-10-16

我正在OpenSees(一个主要用Visual Studio c ++编写的开源地震工程模拟项目)中编写自适应步长更新算法。我在两个不同的头文件(即windef.hsteelz01.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

这不会更改现有库的任何代码。但是,使用通用名称的库作者因此建议他将通用名称保留在命名空间下以减少任何冲突。