0

i want to define dynamic array of struct pointers i have box2d struct :

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;
};

and in the c++ file i want to define array of b2Vec2's when i try to set the array with new b2Vec2 struct im getting the error:

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 .
}

what im doing wrong ?

1
  • 2
    Your code is exception-unsafe and thus bad. Use std::vector. Commented Nov 10, 2013 at 20:49

2 Answers 2

4

You missed two *:

b2Vec2 **vertices = new b2Vec2*[buffer.size()];
       ^                      ^

But, it'd better to use std::vector instead of basre pointers.

std::size_t N = buffer.size();

std::vector<std::vector<b2Vec2>> vertices (N, std::vector<b2Vec2>(N));
Sign up to request clarification or add additional context in comments.

1 Comment

For example vertices[i][j].x = 1.15
2

You might also try not allocating them as pointers at all and using something like an std::vector to do this:

#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
   }

}

The only reason I suggest this is that creating pointers and arrays dynamically often leads to memory leaks, loss of sleep, etc.

If you need them to be returned, it can be returned. It can also be a member of the enclosing class, passed in and initialized by your function. You can change any element by accessing it as vertices[idx]. You don't have to worry about memory leaks...you did not call new on it, so you don't have to call delete on it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.