0

Background

I am self-learning C++ from this course, and I am unsure whether I understand why exactly my syntax was wrong.

Problem

Please see the two versions of the PointArray constructor in geometry_test.cpp. The first one gives an error, while the second one comes from the solution manual. In my (probably flawed) understanding, the two versions should be equivalent. Where have I gone wrong? When am I referring to the member attributes points and size? and when am I referring to the input variables points and size?

Thank you for any clarifications or suggestions to references I should check out.

Related query

How might the error message suggest a solution?

Code

geometry_test.h

class Point {
private:
    int x, y;
public:
    Point(int create_x = 0, int create_y = 0) { x = create_x; y = create_y; };
    int getX() const { return x; };
    int getY() const { return y; };
    void setX(const int new_x) { x = new_x; };
    void setY(const int new_y) { y = new_y; };
}; 

class PointArray {
private:
    Point *points;
    int size;
    void resize(int n);
public:
    PointArray(const Point points[], const int size);
    ~PointArray() { delete[] points; };
};

geometry_test.cpp

#include "geometry.h"
#include <iostream>
using std::cout;

PointArray::PointArray(const Point points[], const int size) {         // breaks
    points = new Point[size]; 
    this->size = size;
    for (int i = 0; i < size; ++i) { this->points[i] = points[i]; }
}

/*
PointArray::PointArray(const Point ptsToCp[], const int toCpSz) {      // works
    points = new Point[toCpSz]; 
    size = toCpSz;
    for (int i = 0; i < toCpSz; ++i) { points[i] = ptsToCp[i]; }
}
*/

int main() {
    Point p1(1, 0);
    Point p2(2, 0);
    Point p3(1, 1);
    Point ptarray[3] = {p1, p2, p3};
    PointArray pa(ptarray, 3);

    cout << p1.getX() << ", " << p1.getY() << "\n";
    cout << p2.getX() << ", " << p2.getY() << "\n";
    cout << p3.getX() << ", " << p3.getY() << "\n";

    return 0;
}

Error message

geometry_test(17467,0x11b51adc0) malloc: *** error for object 0x7ffee4705690: pointer being freed was not allocated
geometry_test(17467,0x11b51adc0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Benjamin Wang
  • 226
  • 1
  • 9
  • 2
    In the first version `points = new Point[size];` overwrites the `points` argument instead of initializing the `this->points` member. – dxiv Jun 27 '20 at 06:51
  • A good rule of thumb is not to have class parameters having the same name as class members – user Jun 27 '20 at 06:55
  • 1
    another problem that could have been avoided by not using pointers – M.M Jun 27 '20 at 06:55
  • Okay! Rewriting ```points = new Point[size];``` as ```this->points = new Point[size];``` fixed it! – Benjamin Wang Jun 27 '20 at 06:55
  • @user I understand that could be a solution. By "class parameters", do you mean the input variables to the class constructor? – Benjamin Wang Jun 27 '20 at 06:56
  • @M.M would you suggest an alternative way to create a ```PointArray``` from some number of ```Point```s? I am willing to learn. I am just taking an intro course to ```C++```. – Benjamin Wang Jun 27 '20 at 07:00
  • Is that a course from MIT? `#define` and references to cplusplus.com Looks like this course needs to be updated to modern development practices. (And cppreference) – JVApen Jun 27 '20 at 07:03
  • 1
    @BenjaminWang The idiomatic way to do this would be to just use `std::vector`. – Kyle Jun 27 '20 at 07:09
  • Does this answer your question? [Members vs method arguments access in C++](https://stackoverflow.com/questions/885136/members-vs-method-arguments-access-in-c) – Korosia Jun 27 '20 at 07:17

0 Answers0