Dreamz
Dreamz

Reputation: 15

Use a member of struct vector in nested classes

I have written some codes like this

struct connectedGrids
{
    int Coord[3];
    enum faceOrien_Type
    {
        xNeg = 0,
        xPos = 1,
        yNeg = 2,
        yPos = 3,
        zNeg = 4,
        zPos = 5
    }faceOrien;
};

class  face
{
public:
    vector<connectedGrids> ConnectedGrids;
};

class grid
{
public:
    face Face;
}

I have initialized an object Grid in the main.cpp

vector<vector<vector<grid> > > Grid = initGrid();

And I want to call the member of struct vector in nested classes like this:

Grid[i][j][k].Face.ConnectedGrids.faceOrien = 1;

But it gave me an error saying

faceOrien is not the member of std::vector<_Ty>

I'm new to C++, and I can't get out where is wrong :(

Upvotes: 0

Views: 247

Answers (3)

Karadur
Karadur

Reputation: 1246

Grid[i][j][k].Face.ConnectedGrids is a vector. It surely doesn't have faceOrien member. You should add something to the vector and then access its elements, for example:

Grid[i][j][k].Face.ConnectedGrids.push_back(connectedGrids());
Grid[i][j][k].Face.ConnectedGrids[someIndex].faceOrien = 1;

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227418

ConnectedGruds is an vector<connectedGrids>, but you are treating it like a connectedGrids object.

Grid[i][j][k].Face.ConnectedGrids[0].faceOrien = 1;
//                               ^^^  assumes size > 0

Upvotes: 2

Useless
Useless

Reputation: 67733

Well, ConnectedGrids is a vector, you declared it as

vector<connectedGrids> ConnectedGrids;

So, which of the connectedGrids structures inside that vector did you want to modify?

Upvotes: 4

Related Questions