结构指针的 C++ 数组
c++ array of struct pointers
我想定义结构指针的动态数组我有 box2d 结构
:struct b2Vec2
{
/// Default constructor does nothing (for performance).
b2Vec2() {}
/// Construct using coordinates.
b2Vec2(float32 x, float32 y) : x(x), y(y) {}
/// Set this vector to all zeros.
void SetZero() { x = 0.0f; y = 0.0f; }
/// Set this vector to some specified coordinates.
void Set(float32 x_, float32 y_) { x = x_; y = y_; }
/// Negate this vector.
b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
/// Read from and indexed element.
float32 operator () (int32 i) const
{
return (&x)[i];
}
/// Write to an indexed element.
float32& operator () (int32 i)
{
return (&x)[i];
}
/// Add a vector to this vector.
void operator += (const b2Vec2& v)
{
x += v.x; y += v.y;
}
/// Subtract a vector from this vector.
void operator -= (const b2Vec2& v)
{
x -= v.x; y -= v.y;
}
/// Multiply this vector by a scalar.
void operator *= (float32 a)
{
x *= a; y *= a;
}
/// Get the length of this vector (the norm).
float32 Length() const
{
return b2Sqrt(x * x + y * y);
}
/// Get the length squared. For performance, use this instead of
/// b2Vec2::Length (if possible).
float32 LengthSquared() const
{
return x * x + y * y;
}
/// Convert this vector into a unit vector. Returns the length.
float32 Normalize()
{
float32 length = Length();
if (length < b2_epsilon)
{
return 0.0f;
}
float32 invLength = 1.0f / length;
x *= invLength;
y *= invLength;
return length;
}
/// Does this vector contain finite coordinates?
bool IsValid() const
{
return b2IsValid(x) && b2IsValid(y);
}
/// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
b2Vec2 Skew() const
{
return b2Vec2(-y, x);
}
float32 x, y;
};
在 C++ 文件中,我想定义 b2Vec2 的数组当我尝试使用新的 b2Vec2 结构设置数组时,我收到错误:
error C2679: binary '=' : no operator found which takes a right-hand operand of type 'b2Vec2 *' (or there is no acceptable conversion)
b2Vec2 *vertices = new b2Vec2[buffer.size()]; // its int number > 0
int verticeslength = polygon->buffer.size();
for (int ii=0, nn=verticeslength; ii<nn; ii++) {
vertices[ii] = new b2Vec2(); // This is where the error .
}
我做错了什么?
你错过了两个*
:
b2Vec2 **vertices = new b2Vec2*[buffer.size()];
^ ^
但是,最好使用std::vector
而不是基础指针。
std::size_t N = buffer.size();
std::vector<std::vector<b2Vec2>> vertices (N, std::vector<b2Vec2>(N));
您也可以尝试根本不将它们分配为指针,而是使用 std::vector 之类的东西来执行此操作:
#include <vector>
using namespace std;
...
{
vector<b2Vec2> vertices;
vertices.resize(buffer.size());
// Use the vertices.
for(int idx = 0; idx < vertices.size(); idx++)
{ // Do something with each vertex
}
}
我建议这样做的唯一原因是动态创建指针和数组通常会导致内存泄漏、睡眠不足等。
如果您需要退回它们,可以将其退回。 它也可以是封闭类的成员,由函数传入和初始化。 您可以通过将任何元素作为顶点[idx]访问来更改它。 您不必担心内存泄漏...您没有对其调用 new,因此不必对其调用 delete。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '