error: expected unqualified-id before ‘class’

error: expected unqualified-id before ‘class’

在 XVisualInfo 结构中有一个名为 class 的 属性, 当我想在 C++ 程序中使用这个结构时出现问题:

...
XVisualInfo templ;
templ.screen = screen;
templ.depth = 32;
templ.class = TrueColor;
...

当我尝试编译时出现以下错误:

error: expected unqualified-id before ‘class’
templ.class = TrueColor;
      ^~~~~

现在我该怎么做才能让它发挥作用?!!

这是来自 /usr/include/X11/Xutil.h

的 XVisualInfo 的实际定义
typedef struct {
  Visual *visual;
  VisualID visualid;
  int screen;
  int depth;
#if defined(__cplusplus) || defined(c_plusplus)
  int c_class;                  /* C++ */
#else
  int class;
#endif
  unsigned long red_mask;
  unsigned long green_mask;
  unsigned long blue_mask;
  int colormap_size;
  int bits_per_rgb;
} XVisualInfo;

如您所见,编写 C++ 代码的规定就在那里。只需使用 c_class 成员名称。