gpt4 book ai didi

c++ - C++ 中的命名冲突 : How to access a struct member called "class"

转载 作者:IT老高 更新时间:2023-10-28 22:16:41 26 4
gpt4 key购买 nike

我在使用 xlib 库时遇到了一个命名问题:

我正在使用一个结构,它有一个名为“类”的成员。我假设这个库主要用于纯 C 程序。所以没有问题。

但我正在用 C++ 编程,这里的名称“类”是一个关键字,不能用来表示变量。所以,如果我通过

访问结构

myvariable = mystruct->class;

我收到了错误:

“class”之前的预期 unqualified-id

鉴于我无法更改结构本身,尽管存在命名冲突,我如何访问该结构成员?

最佳答案

Given that I cannot change the struct itself, how can I access this struct member despite the naming conflict?

也许您可以使用#define 重命名它,例如

#define class xclass
#include "header.h"
#undef class

// ...

myvariable = mystruct->xclass;

关于c++ - C++ 中的命名冲突 : How to access a struct member called "class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621812/

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