[英]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? 我错过了什么,或者我试图做不到的事情?
Verts[3]
is of type VertStruct
and Number
is an int
so the assignment is not possible (with the posted code). Verts[3]
的类型为VertStruct
, Number
为int
因此无法进行赋值(使用已发布的代码)。 You could specify one of the members of VertStruct
as the target of the assignment: 您可以指定VertStruct
一个成员作为赋值的目标:
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: 如果你想能够为VertStruct
分配一个int
,你提供了一个operator=(int)
(如Luchian已经提到的那样)但是在我看来 ,在调用站点上对于int
成员来说会很模糊值被分配给:
struct VertStruct
{
double X, Y, Z;
VertStruct& operator=(int d)
{
X = d; // Or 'Y' or 'Z' or all.
return *this;
}
};
This 这个
Chunk[5].LOD[4].Verts[3]
is a VertStruct
, and you cannot assign an integer to it. 是VertStruct
,您不能为其分配整数。
You are missing something, and it's not impossible (everything's possible if you can imagine it. That's what being a scientist is all about) :) 你错过了什么,这并非不可能(如果你能想象的话,一切皆有可能。那就是科学家的全部意义):)
Chunk[5]
returns a ChunkStruct
. Chunk[5]
返回一个ChunkStruct
。
Chunk[5].LOD
returns a std::vector<LODStruct>
. Chunk[5].LOD
返回一个std::vector<LODStruct>
。
Chunk[5].LOD[4]
returns a LODStruct
. Chunk[5].LOD[4]
返回LODStruct
。
Chunk[5].LOD[4].Verts
returns a std::vector<VertStruct>
. Chunk[5].LOD[4].Verts
返回std::vector<VertStruct>
。
Finally, Chunk[5].LOD[4].Verts[3]
returns a VertStruct
. 最后, Chunk[5].LOD[4].Verts[3]
返回一个VertStruct
。 And you try to assign an int
to it. 并尝试为其分配一个int
。 Obviously, not gonna work with your code. 显然,不能使用你的代码。
To get this to work (this exact syntax), you should overload operator = (int x)
. 为了使这个工作(这个确切的语法),你应该重载operator = (int x)
。 Otherwise, assign the int
to whichever member you want. 否则,将int
分配给您想要的任何成员。
You are assigning one number which is an int
into VertStruct
, which is a struct
and has 3 fields (X, Y, Z). 您将一个int
数字分配给VertStruct
,它是一个struct
,有3个字段(X,Y,Z)。 If you wanted to set X coordinate, do: 如果要设置X坐标,请执行以下操作:
Chunk[5].LOD[5].Verts[3].X = Number;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.