代码之家  ›  专栏  ›  技术社区  ›  armagedescu

内联结构初始化,“非静态成员必须相对于静态对象”

  •  0
  • armagedescu  · 技术社区  · 12 月前

    我在从嵌套结构中引用外部结构成员时遇到了一个小问题。当我试图设置 x y width height 它显示了错误“非静态成员必须相对于静态对象”,以下是代码:

    const int width = 1280, height = 720;
    struct MandelbrotBase
    {
        int width = ::width, height = ::height;
        ...
        struct
        {
            int x = width / 2;
            int y = height / 2;
        } pos;
    } mb;
    __global__ void generate_mandelbrot(unsigned int* colors, MandelbrotBase mb)
    {
    ...
    }
    

    这不是一个大问题,因为我可以参考全球 ::width ::heigth 而是使用其他方法进行初始化。但是,有没有可能直接在线完成呢?使用成员构造函数或函数不是一种选择,至少不是首选,因为我用它来向cuda内核函数传递信息,所以我想保持它非常简单。

    1 回复  |  直到 12 月前
        1
  •  2
  •   3CxEZiVlQ    12 月前

    你可以让它像

    const int width = 1280, height = 720;
    
    struct MandelbrotBase {
      int width = ::width, height = ::height;
      struct {
        int x;
        int y;
      } pos = {.x = width / 2, .y = height / 2};
    } mb;
    
    void generate_mandelbrot(unsigned int* colors, MandelbrotBase mb) {}
    

    非静态 width height 不能在外部引用 MandelbrotBase 没有对象的成员。未命名的结构不是该结构的一部分 MandelbrotBase 定义,未命名的结构体只是在结构体中定义的 MandelbrotBase 名称范围。