Hello. How to solve this error ?!

Mistake:

In member function 'void Blocks::SetCube(std::vector<Texture2f>*, std::vector<Vector3f>*, std::vector<unsigned int>*, char)': /home/hays/program/WorldMap/Blocks.cpp:112:55: error: base operand of '->' has non-pointer type 'std::vector<Texture2f>' TextureBuffer[TextureBuffer->size()-1]->u = CubeGrass.TextureData[i].u; ^ /home/hays/program/WorldMap/Blocks.cpp:113:55: base operand of '->' has non-pointer type 'std::vector<Texture2f>' TextureBuffer[TextureBuffer->size()-1]->v = CubeGrass.TextureData[i].v; ^ /home/hays/program/WorldMap/Blocks.cpp:119:54: base operand of '->' has non-pointer type 'std::vector<Vector3f>' VertexBuffer[TextureBuffer->size()-1]->x = CubeGrass.VertexData[i].x; ^ /home/hays/program/WorldMap/Blocks.cpp:120:54: base operand of '->' has non-pointer type 'std::vector<Vector3f>' VertexBuffer[TextureBuffer->size()-1]->y = CubeGrass.VertexData[i].y; ^ /home/hays/program/WorldMap/Blocks.cpp:121:54: base operand of '->' has non-pointer type 'std::vector<Vector3f>' VertexBuffer[TextureBuffer->size()-1]->z = CubeGrass.VertexData[i].z; ^ /home/hays/program/WorldMap/Blocks.cpp:128:50: no match for 'operator=' (operand types are 'std::vector<unsigned int>' and 'unsigned int')[/COLOR] IndexBuffer[TextureBuffer->size()-1] = CubeGrass.IndexData[i]; 

Structures:

 typedef float Scalar; struct Vector3f { Scalar x, y, z; }; struct Texture2f { Scalar u, v; }; 

Function:

 using namespace std; void Blocks::SetCube(vector <Texture2f> *TextureBuffer,vector <Vector3f> *VertexBuffer, vector <GLuint> *IndexBuffer,char type) { switch(type) { case 0: for(int i(0);i<24;i++) { TextureBuffer->push_back(Texture2f()); TextureBuffer[TextureBuffer->size()-1]->u = CubeGrass.TextureData[i].u; TextureBuffer[TextureBuffer->size()-1]->v = CubeGrass.TextureData[i].v; } for( int i(0);i<24;i++) { VertexBuffer->push_back(Vector3f()); VertexBuffer[TextureBuffer->size()-1]->x = CubeGrass.VertexData[i].x; VertexBuffer[TextureBuffer->size()-1]->y = CubeGrass.VertexData[i].y; VertexBuffer[TextureBuffer->size()-1]->z = CubeGrass.VertexData[i].z; } for(int i(0);i<36;i++) { IndexBuffer->push_back(GLuint()); IndexBuffer[TextureBuffer->size()-1] = CubeGrass.IndexData[i]; } break; } } 

    2 answers 2

    The value extracted from the vector is not a pointer, but a link. Instead

     TextureBuffer[TextureBuffer->size()-1]->u = CubeGrass.TextureData[i].u; 

    should write

     TextureBuffer[TextureBuffer->size()-1].u = CubeGrass.TextureData[i].u; 

    In general, instead of adding an element, and then for each change of its field to get it again from the vector, it would be better to first set the field values, and then add:

     Texture2f tex; tex.u = CubeGrass.TextureData[i].u; tex.v = CubeGrass.TextureData[i].v; TextureBuffer->push_back(text); 

    Well, you can pass vectors not by a pointer, but by reference:

     void Blocks::SetCube( vector<Texture2f>& TextureBuffer, vector<Vector3f>& VertexBuffer, vector<GLuint>& IndexBuffer, char type) 

    Then you can instead -> use . :

     Texture2f tex; tex.u = CubeGrass.TextureData[i].u; tex.v = CubeGrass.TextureData[i].v; TextureBuffer.push_back(text); 

    In my opinion, aesthetic.

    • Visited with TextureBuffer [TextureBuffer-> size () - 1] .u = CubeGrass.TextureData [i] .u; error of type /home/hays/program/WorldMap/Blocks.cpp:112:56: error: 'class std :: vector <Texture2f>' has no member named 'u' TextureBuffer [TextureBuffer-> size () - 1]. u = CubeGrass.TextureData [i] .u; ^ /home/hays/program/WorldMap/Blocks.cpp:113:56: error: 'class std :: vector <Texture2f>' has no member named 'v' TextureBuffer [TextureBuffer-> size () - 1] .v = CubeGrass.TextureData [i] .v; - hays
    • @hays Add text errors to the question in formatted form. At the same time make sure that the class really has such a field. Well, update the code in accordance with at least the first advice from the answer (after "and then added") - the error will be better seen. - Athari

    Why do you pass a pointer to a vector in a function? Pass the link and problems with the syntax will not. And now you are doing wrong:

     TextureBuffer[TextureBuffer->size()-1]->u = CubeGrass.TextureData[i].u; 

    Literally means the following: (TextureBuffer + TextureBuffer->size() - 1)->u - this is undefined behavior (undefined behavior), because You got into the memory, which is not clear where. It will be correct to write like this:

     *(TextureBuffer)[TextureBuffer->size()-1].u = CubeGrass.TextureData[i].u; 

    or so:

     TextureBuffer->back().u = CubeGrass.TextureData[i].u;