Trt Trt
Trt Trt

Reputation: 5552

C++ array in structure access

I have this structure in c++:

struct Vertex_2 {
    GLdouble position[3];
};

I am trying to access the array inside of it like this:

Vertex_2.position[0] = //something;
Vertex_2.position[1] = //something;
....
...
..

when I compile it I get this:

error: expected unqualified-id before ‘.’ token

why is that?

Upvotes: 0

Views: 1825

Answers (3)

enticedwanderer
enticedwanderer

Reputation: 4346

Because you are trying to access struct type not the actual struct. Try:

struct Vertex_2 {
    GLdouble position[3];
} myVertex;

myVertex.position[0] = //something;
myVertex.position[1] = //something;

Upvotes: 0

Seth Carnegie
Seth Carnegie

Reputation: 75150

You have to create an instance of the struct before using the members thereof.

Vertex_2 v; // v is an *instance* of the *struct* Vertex_2
v.position[0] = //something;
v.position[1] = //something;
...

Think of Vertex_2 as a description of what all Vertex_2's should look like (but it is not, itself, a Vertex_2). Then you have to actually create a Vertex_2, by doing Vertex_2 name;. In the example, we used the name v instead of name, but you can name the instance whatever you want. Then you can access the member variables of that instance through the name with a dot (.), like you tried to do before.

Upvotes: 2

littleadv
littleadv

Reputation: 20282

You need to define a variable of your class, you only defined a type.

struct Vertex_2 {
    GLdouble position[3];
} varVertex_2; // <-- now you have an instance of the struct


varVertex_2.position[0] = //something;
varVertex_2.position[1] = //something;

Upvotes: 1

Related Questions