mardi 24 avril 2018

C++ anonymous union redeclaration error

I want to create a struct that can be used to store 3D coordinates or a linear equation. Here is the code:

struct myStruct {
    union {
        // coordinates (3d)
        struct {
            int x,y,z;
        };
        // linear equation (ax+b)
        struct {
            int a,b,x;
        };
    };
};

And I get the following error:

error: redeclaration of ‘int myStruct::<anonymous union>::<anonymous struct>::x’

I'm on linux mint 18.04, g++ (5.4.0), compile with --std=c++11.

I understand the problem. But have few questions.

  1. I saw something related working on windows, why?
  2. What is the best way to implement it so it works well on both (linux/win)?

Aucun commentaire:

Enregistrer un commentaire