4

This question is my mistake. The code described below is being built well with no problem.


I have this class.

Vector.h

struct  Vector
{
    union
    {
        float   elements[4];
        struct
        {
            float   x;
            float   y;
            float   z;
            float   w;
        };                  
    };

    float   length();
}

Vector.cpp

float Vector::length()
{
  return x;  // error: 'x' was not declared in this scope
}

How to access the member x,y,z,w?

2
  • 1
    This compiles fine for me (with a semicolon after struct Vector) in g++ 4.4.0. See ideone.com/m5tVu.
    – TonyK
    Feb 5, 2011 at 16:03
  • @TonyK I apology for my mistake. It's compiled well. I can't figure out why it was not compiled before... Sorry and thanks everyone!
    – eonil
    Feb 6, 2011 at 13:53

2 Answers 2

5

You need an instance of your struct inside the anonymous union. I don't know exactly what you want to achive, but e.g. something like this would work:

struct Vector
{
  union
  {
    float elements[4];
    struct
    {
      float x, y, z, w;
    }aMember;
  };

  float length() const
  {
    return aMember.x;
  }
};
11
  • 1
    The error is occurring in a member function, where there's implicitly an instance. I think this is probably due to something else. Feb 5, 2011 at 11:17
  • @templatedtypedef: I was talking of the struct inside the union. And I updated my answer so please take away your downvote. Feb 5, 2011 at 11:30
  • 1
    The reason why you need a named member is that there are no anonymous structs in C++. union { ... }; defines an unnamed type and an unnamed object, but struct { ... }; defines only an unnamed type. I think the OP's code might even be illegal because the standard draft says that an anonymous union must not contain type declarations.
    – Philipp
    Feb 5, 2011 at 12:48
  • 1
    I'm kinda wondering.. I use anonymous structs for my math library as well, and there's no compiler error. struct Vector3{ union{ struct{ float x; float y; float z; }; float a[3]; }; }; I can access x, y and z either directly by their name or with a[0], a[1], a[2].
    – Xeo
    Feb 5, 2011 at 15:43
  • 1
    @Eonil My code compiled successfull as C++ – what compiler are you using? Feb 6, 2011 at 13:38
4

What you have created is not an anonymous member, but anonymous type (which is useless by itself). You have to create a member of your anonymous type. This concerns both your struct and your union.

Adjust the header like this:

struct  Vector
{
    union
    {
        float   elements[4];
        struct
        {
            float   x;
            float   y;
            float   z;
            float   w;
        } v;
    } u;

    float   length();
};

Now you can access your members like this:

u.elements[0] = 0.5f;
if(u.v.x == 0.5f) // this will pass
    doStuff();
2
  • 1
    Why don't you want let him using anonymous unions? It's absolutely no problem and sometimes even recommended (devx.com/tips/Tip/12470) Feb 5, 2011 at 11:27
  • @Polybos Good point, I didn't even know about anonymous unions until now :)
    – Fiktik
    Feb 5, 2011 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.