Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Inline struct initialization, "nonstatic member must be relative to a static object"

I have a small issue with referencing the outer struct members from a nested struct. When I try to set x and y to width and height it shows the error "a nonstatic member must be relative to a static object", here is the code:

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)
{
...
}

Is not that a big problem, since I can reference the global ::width and ::heigth instead, or other methods to initialize. But, is there any possibility to do do it just inline? Using a member constructor or function is not an option, at least is not a preferred option, because I use it to pass information to cuda kernel functions, so I want to keep it very simple.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can make it like

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) {}

Non static width and height can’t be referenced outside MandelbrotBase members without an object. The unnamed struct is not a part of the struct MandelbrotBase definition, the unnamed struct is just defined in the struct MandelbrotBase name scope.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading