C++ 拥有构造函数会导致错误“;错误:无操作员“=&引用;匹配这些操作数”;
所以,我有一个C++ 拥有构造函数会导致错误“;错误:无操作员“=&引用;匹配这些操作数”;,c++,constructor,C++,Constructor,所以,我有一个structmesh,它包含一个std::向量的三角形,它包含向量 template <typename T> struct genericVec3d { T x = 0, y = 0, z = 0, w = 1; //////constructors genericVec3d() {}; genericVec3d(T _x, T _y, T _z, T _w) :x(_x), y(_y), z(_z), w(_w) {}; g
struct
mesh,它包含一个std::向量的三角形,它包含向量
template <typename T>
struct genericVec3d
{
T x = 0, y = 0, z = 0, w = 1;
//////constructors
genericVec3d() {};
genericVec3d(T _x, T _y, T _z, T _w) :x(_x), y(_y), z(_z), w(_w) {};
genericVec3d(T _x, T _y, T _z) :x(_x), y(_y), z(_z) {};
genericVec3d(const genericVec3d& other) : x(other.x), y(other.y), z(other.z) {};
};
typedef genericVec3d<float> vf3d;
struct triangle
{
vf3d p[3];
};
struct mesh
{
std::vector<triangle> tris;
};
我在sheet.tris={{{1,1,1}行中得到一条错误消息no operator“=”与这些操作数匹配代码>
如果我注释掉所有的构造函数,它运行得很好,但是有任何构造函数都会导致错误,我不知道为什么。我需要这些构造函数,以便我的其余代码正常工作。我能做什么
可复制代码
#include <vector>
template <typename T>
struct genericVec3d
{
T x = 0, y = 0, z = 0, w = 1;
//////constructors
genericVec3d() {};
genericVec3d(T _x, T _y, T _z, T _w) :x(_x), y(_y), z(_z), w(_w) {};
genericVec3d(T _x, T _y, T _z) :x(_x), y(_y), z(_z) {};
genericVec3d(const genericVec3d& other) : x(other.x), y(other.y), z(other.z) {};
};
typedef genericVec3d<float> vf3d;
struct triangle
{
vf3d p[3];
};
struct mesh
{
std::vector<triangle> tris;
};
int main()
{
mesh sheet;
sheet.tris = { {1, 1, 1} };
}
#包括
模板
结构genericVec3d
{
T x=0,y=0,z=0,w=1;
//////建设者
genericVec3d(){};
广义向量3d(T_x,T_y,T_z,T_w):x(x),y(y),z(z),w(w){};
广义向量3d(T_x,T_y,T_z):x(x),y(y),z(z){};
genericVec3d(const genericVec3d&other):x(other.x)、y(other.y)、z(other.z){};
};
typedef genericVec3d vf3d;
结构三角形
{
vf3dp[3];
};
结构网格
{
std::向量tris;
};
int main()
{
网片;
sheet.tris={1,1,1};
}
您需要更多的{}
,因为您刚才所做的是用一个三角形创建一个向量,而这个三角形试图在只有1个(数组)的情况下设置3个变量
//你有什么
sheet.tris={//三角形向量
{//三角形
1,//表示是vf3ds的数组,但它是int
1, // ?
1 // ?
}
};
//你需要什么
sheet.tris={//三角形向量
{//三角形
{//vf3ds的数组
{1.f,1.f,1.f}//vf3ds
//理想情况下,这里应该还有两个VF3D,
//因为三角形存储了3个vf3ds的数组
}
}
};
错误消息应该准确地告诉您要传递给运算符=
的操作数。您可能需要向triangle
和genericVec3d
添加一些额外的构造函数和赋值运算符来处理(std::vector
已经有了它们)。
#include <vector>
template <typename T>
struct genericVec3d
{
T x = 0, y = 0, z = 0, w = 1;
//////constructors
genericVec3d() {};
genericVec3d(T _x, T _y, T _z, T _w) :x(_x), y(_y), z(_z), w(_w) {};
genericVec3d(T _x, T _y, T _z) :x(_x), y(_y), z(_z) {};
genericVec3d(const genericVec3d& other) : x(other.x), y(other.y), z(other.z) {};
};
typedef genericVec3d<float> vf3d;
struct triangle
{
vf3d p[3];
};
struct mesh
{
std::vector<triangle> tris;
};
int main()
{
mesh sheet;
sheet.tris = { {1, 1, 1} };
}