我是新手,在C ++中存在问题。我正在创建3D坐标,(面部每个角落的x,y和z,然后是6个面)并收到许多错误。这是我的代码:
#include <vector>
int main()
{
std::vector<int> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
int x1 = 0;
int y1 = 1;
int z1 = 2;
int x2 = 3;
int y2 = 4;
int z2 = 5;
xyzCoords = {
{ x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2 },
{ x1, y2, z1, x2, y2, z1, x2, y2, z2, x1, y2, z2 },
{ x1, y2, z1, x1, y1, z1, x1, y1, z2, x1, y2, z2 },
{ x2, y2, z1, x2, y1, z1, x2, y1, z2, x2, y2, z2 },
{ x1, y2, z2, x1, y1, z2, x2, y1, z2, x2, y2, z2 },
{ x1, y2, z1, x1, y1, z1, x2, y1, z1, x2, y2, z1 }
};
return 0;
}
这是出现问题的代码。你可以看到我将xyzCoords
定义为向量。我不确定这是否是正确的方法。我也不想单独定义xyz123
。实现这一目标的最佳方法是什么?我要使用列表,数组还是向量?请写下如何执行此操作的代码。谢谢!
错误:
E0289:没有构造函数的实例“std :: vector&lt; _Ty,_Alloc&gt; :: vector [with _Ty = int,_Alloc = std :: allocator]”匹配参数列表
E0349:没有运算符“=”匹配这些操作数
C2440:'初始化':无法从'初始化列表'转换为'std :: vector&gt;'
C2679:binary'=':找不到哪个运算符采用'初始化列表'类型的右手操作数(或者没有可接受的转换)
答案 0 :(得分:2)
也许您应该将代码重构为类似的内容,以获得可见性和更好的理解:
#include <vector>
int main()
{
struct Corner
{
int x;
int y;
int z;
Corner(int x, int y, int z) :
x(x), y(y), z(z) {}
};
struct Face
{
Corner c1;
Corner c2;
Corner c3;
Corner c4;
Face(Corner c1, Corner c2, Corner c3, Corner c4) :
c1(c1), c2(c2), c3(c3), c4(c4) {}
};
Corner c1(0, 1, 2);
Corner c2(3, 4, 5);
Face f1(c1, c2, c1, c2);
std::vector<Face> faces = { f1, f1, f1, f1, f1, f1 };
return 0;
}
这将创建6个面的数组,其角坐标为(x1,y1,z1)(x2,y2,z2)(x1,y1,z1)(x2,y2,z2)
答案 1 :(得分:2)
您的初始化与您声明的类型不匹配。声明一维数组,而初始化是二维数组。
std::vector<int> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
应该是:
std::vector<std::vector<int>> xyzCoords = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
您提到了xyz
坐标,所以为什么不制作专门用于坐标的课程,例如:
struct Point {
int x, y, z;
};
无论如何,我不建议您使用朴素的方法解决这个问题,因为它将是一个矩阵操作密集型计算。您应该使用BLAS / LAPACK库或其Eigen或Armadillo之类的包装器。他们肯定要快得多。
答案 2 :(得分:2)
#include <vector>
struct Vertex {
float x_; // can change these to int
float y_;
float z_;
Vertex() : x_(0), y_(0), z_(0) {}
Vertex( float x, float y, float z ) : x_(x), y_(y), z_(z) {}
explicit Vertex( float val ) : x_(val), y_(val), z_(val) {}
};
struct Face {
Vertex v0_;
Vertex v1_;
Vertex v2_;
Vertex v3_;
Face() :
v0_(Vertex()),
v1_(Vertex()),
v2_(Vertex()),
v3_(Vertex()) {
}
Face( Vertex v0, Vertex v1, Vertex v2, Vertex v3 ) :
v0_(v0),
v1_(v1),
v2_(v2),
v3_(v3) {
}
};
int main() {
std::vector<Face> faces;
// Not exact values the OP is looking for,
// just a quick way to populate the vector of faces
// for demonstration purposes of the usefulness of structs & classes.
for ( unsigned n = 1; n <= 6; n++ ) {
faces.push_back( Face( Vertex(n), Vertex(n+1), Vertex(n+2), Vertex(n+3) ) );
}
return 0;
}