Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your parameterised constructor of <code>Shape</code> does this:</p> <pre><code>vertexArray = vertices; </code></pre> <p>Where <code>vertices</code> is a function parameter declared as <code>double vertices[]</code>. This is syntactic sugar to <code>double *vertices</code>, i.e., you're copying a <em>pointer</em> (because arrays decay to pointers when passed to functions).</p> <p><code>vertexArray</code> is defined as a pointer inside the class. You even mention it's a "dynamic array," but then you initialise it with an array allocated on the stack (a local variable inside <code>display()</code>). This makes the pointer dangling.</p> <p>You will have to make <code>vertexArray</code> an array and copy the data, not just the pointer. Something like this:</p> <pre><code>class Shape { // ... double vertexArray[4]; // ... }; Shape::Shape(int shapeType, double vertices[], int color[]) { type = shapeType; if (shapeType==0) //shape is a rectangle { numVertices = 4; } else if(shapeType==1) //shape is a circle { //shape is a circle, therefore the second array param is in fact an array of size 2 containing the (x, y)-coordinates of the circle origin... center = vertices; } else if (shapeType==2) //shape is a triangle { numVertices = 3; } if (shapeType!=1) //as long as shape is NOT a circle, interpret the second constructor parameter as a list of vertices { std::copy(vertices, vertices + numVertices, vertexArray); } } </code></pre> <p>And analogically for <code>color</code> as well, of course.</p> <p><strong>Extra suggestions</strong></p> <p>Of course, this code would be much better using <code>std::vector</code> or something similar instead of plain arrays, using and <code>enum</code> instead of <code>int</code> for <code>shapeType</code> etc. I suggest you pick up a <a href="https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list">good C++ book</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload