C++ copy constructor, destructor error and more -
i new c++. have tried figure out how copy constructor works , destructor, cant work wright.
i 2 errors:
error: expected primary-expression before ';' token
delete [];
- i have tried put different things after delete [] none of them worked, left blank @ moment.
class dynamicline has no member named 'p2',
- what doing wrong? did decleare p2 in test program.
this header file:
template<class t> class dline { public: dline (t *v1, t *v2) : v1 {v1}, v2 {v2} {} t* v1; t* v2; // copy constructor dline(const dline &l) { v1 = l.v1; v2 = l.v2; } dline& operator= (const dline &l) { if (this == &l) { return *this; } v1 = l.v1; v2 = l.v2; return *this; } ~dline() { delete []; } };
i have vector class:
using namespace std; vector2::vector2(float nx, float ny) : x {nx} , y {ny} { } float vector2::distancefrom(vector2 v) { return sqrt( (x - v.x)*(x - v.x) + (y - v.y)*(y - v.y) ); } ostream& operator << (ostream& os, vector2 v) { return os << "(" << v.x << "," << v.y << ")"; }
and part of test program:
vector2 p1 {3.0, 5.0}; vector2 p2 {0,0}; vector2 p3 {7.0, 8.0}; dline<vector2> l1 {&p1, &p2}; dline<vector2> l2 {nullptr, nullptr}; l2 = l1; l2.p2 = &p3; p1.x = 2.0;
delete
used release memory that's dynamically allocated.v1
,v2
members of classdline
,p2
variable declared in program.
Comments
Post a Comment