gpt4 book ai didi

c++,是否可能在不同的头文件中有两个同名的 union

转载 作者:行者123 更新时间:2023-11-30 01:15:08 25 4
gpt4 key购买 nike

系统中有两个头文件,我需要包含这两个头文件才能与系统交互,这两个头文件分别是agentRegistrationUnioncounterCollectUnionContent.h,这两个头文件没有什么特别的,只有两个union,但这两个union同名,唯一不同的是union的内容,union长这样:

union SIGNAL
{
struct s1 ss1;
struct s2 ss2;
......
};

我无法更改这些头文件,当我包含这些头文件时,我得到了错误:'union SIGNAL'的重新定义,我如何在我的代码中同时拥有两个 union ?

我的头文件:

#include <vector>
#include <map>
#include <string>
#include <stdint.h>

#include "agentRegistrationUnion.h" //I only want to separate these two files
#include "counterCollectUnion.h"

using namespace std;

class InvalidSig;

struct args{
InvalidSig* context;
string mname;
union SIGNAL *content;
};

class InvalidSig{

public:
InvalidSig();
~InvalidSig();
void Send_inval_sig();
void Kill_proc();
bool InitAttacker(char *argv);
void LocateVictims();
itc_msg* AllocMsg(union SIGNAL *content);
void PrintVictims(itc_mbox_id_t vic);
void *sendmsg(void *mname);

static void *sendmsg_helper(void *ar){
string n = ((args*)ar)->mname;
return ((args*)ar)->context->sendmsg(&n);
};


multimap<string,string> mboxes;
union SIGNAL *content;

};

最佳答案

agentRegistrationUnion.h中:

namespace Agent {  // or some appropriate name
union SIGNAL { ... };
}

counterCollectUnion.h 中:

namespace Counter {  // or some appropriate name
union SIGNAL { ... };
}

在使用过程中,通过命名空间前缀来引用想要的 union 体:

Agent::SIGNAL signal;
signal.ss1 = ...;

如果您不能更改这些头文件,那么这应该可行:

namespace Agent {
#include "agentRegistrationUnion.h"
}

namespace Counter {
#include "counterCollectUnion.h"
}

Agent::SIGNAL agentSignal;
Counter::SIGNAL counterSignal;

关于c++,是否可能在不同的头文件中有两个同名的 union ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29104509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com