Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectors and Structs in C++

I am trying to make a data tree that holds multiple data types and vectors. What I have so far is shown below:

struct VertStruct{
    double X, Y, Z;
};
struct FaceStruct{
    int F1, F2, F3, F4;
};
struct FaceNormalStruct{
    double X, Y, Z;
};
struct LODStruct{
    std::vector<VertStruct> Verts;
    std::vector<FaceStruct> Faces;
    std::vector<FaceNormalStruct> FaceNormals;
};
struct ChunkStruct{
    std::vector<LODStruct> LOD;
};

int main(){

    std::vector<ChunkStruct> Chunk;
    Chunk.resize(100);

    for(int i = 0; i < 100; i++)
    {
        Chunk[i].LOD.resize(5);

        for(int j = 0; j < 5; j++)
        {
            Chunk[i].LOD[j].Verts.resize(36);
            Chunk[i].LOD[j].Faces.resize(25);
            Chunk[i].LOD[j].FaceNormals.resize(25);
        }
    }
return 1;
}

Now this compiles fine and is exactly what I want, however, if I try to set a value to something like:

int Number = 42;
Chunk[5].LOD[4].Verts[3] = Number;

Then I get the following error:

 main.cpp|126|error: no match for 'operator=' in 'Chunk.std::vector<_Tp, _Alloc>::operator[] [with _Tp = ChunkStruct, _Alloc = std::allocator<ChunkStruct>](5u)->ChunkStruct::LOD.std::vector<_Tp, _Alloc>::operator[] [with _Tp = LODStruct, _Alloc = std::allocator<LODStruct>](4u)->LODStruct::Verts.std::vector<_Tp, _Alloc>::operator[] [with _Tp = VertStruct, _Alloc = std::allocator<VertStruct>](3u) = Number'|

So am I missing something or is what I am attempting to do not possible?

like image 371
LucasS Avatar asked Dec 20 '25 11:12

LucasS


2 Answers

Verts[3] is of type VertStruct and Number is an int so the assignment is not possible (with the posted code). You could specify one of the members of VertStruct as the target of the assignment:

Chunk[5].LOD[4].Verts[3].X = Number;

If you wanted to be able to assign an int to a VertStruct you provide an operator=(int) (as mentioned already by Luchian) but it seems to me it would be quite ambiguous at the call site to what member(s) the int value is being assigned to:

struct VertStruct
{
    double X, Y, Z;
    VertStruct& operator=(int d)
    {
        X = d; // Or 'Y' or 'Z' or all.
        return *this;
    }
};
like image 184
hmjd Avatar answered Dec 23 '25 01:12

hmjd


This

Chunk[5].LOD[4].Verts[3]

is a VertStruct, and you cannot assign an integer to it.

like image 26
juanchopanza Avatar answered Dec 23 '25 02:12

juanchopanza