如何解析具有相同名称的两个结构? [英] How to resolve two structures with the same name?

查看:37
本文介绍了如何解析具有相同名称的两个结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码库中,我发现两个模块具有相同名称的结构.它给出了名称冲突错误.有没有不改代码的办法解决?

In my code base I find that two modules have structures with the same name. It is giving a name conflict error. Is there a way to resolve it without changing the code?

推荐答案

这是一个可怕的 hack,但是可以使用宏来重新定义结构的名称,就像这样

This is a terrible hack, but it would be possible to use a macro to redefine the name of the struct, like so

// a.h
struct collide {
    int a;
};

// b.h
struct collide {
    float b;
};

// test.c
#define collide a_collide
#include "a.h"
#undef collide
#include "b.h"
int main(){
    struct a_collide a;
    struct collide b;
    return 0;
}

当有人不可避免地使用错误的结构时,您可能想要重命名两个标头的结构以给出错误,可能是在像

You'd probably want to rename the struct for both headers to give errors when someone inevitably uses the wrong struct, perhaps in a wrapper header like

// wrap_a.h
#define collide a_collide
#include "a.h"
#undef collide

记住取消定义宏,以免在整个代码中随机替换.

Remember to undef the macro so you don't get random replacements throughout your code.

这篇关于如何解析具有相同名称的两个结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆