This is my code which produces an error (the real cause/remedy for the error is in P.S.2, but I'm still asking for a reason):
#include "stdafx.h"
#include <vector>
using namespace std;
class V2 {
public:
long double x, y;
V2(long double xx = 0, long double yy = 0);
friend V2 operator+(const V2 &v1, const V2 &v2);
friend V2 operator-(const V2 &v1, const V2 &v2);
friend V2 operator*(const long double &r, const V2 &v);
friend V2 operator*(const V2 &v, const long double &r);
friend V2 operator/(const V2 &v, const long double &r);
};
V2::V2(long double X, long double Y) { x = X; y = Y; }
V2 operator+(const V2 &v1, const V2 &v2) { return V2(v1.x + v2.x, v1.y + v2.y); }
V2 operator-(const V2 &v1, const V2 &v2) { return V2(v1.x - v2.x, v1.y - v2.y); }
V2 operator*(const long double &r, const V2 &v) { return V2(r*v.x, r*v.y); }
V2 operator*(const V2 &v, const long double &r) { return V2(r*v.x, r*v.y); }
V2 operator/(const V2 &v, const long double &r) { return V2(v.x / r, v.y / r); }
long double Dot(const V2 &v1, const V2 &v2) { return v1.x*v2.x + v1.y*v2.y; }
long double Cross(const V2 &v1, const V2 &v2) { return v1.x*v2.y - v1.y*v2.x; }
long double DistanceSqr(const V2 &v1, const V2 &v2) { return Dot(v1 - v2, v1 - v2); }
long double Distance(const V2 &v1, const V2 &v2) { return sqrt(Dot(v1 - v2, v1 - v2)); }
class Face {
public:
unsigned int v1, v2, v3;
Face(int vv1 = 0, int vv2 = 0, int vv3 = 0);
};
Face::Face(int vv1, int vv2, int vv3) {
v1 = vv1; v2 = vv2; v3 = vv3;
}
class LineSegment {
public:
unsigned int v1, v2;
LineSegment(int seg1 = 0, int seg2 = 0);
};
LineSegment::LineSegment(int seg1, int seg2) {
v1 = seg1; v2 = seg2;
}
class Boundary2D {
private:
long double threshold = 1e-7;
unsigned int i, j;
public:
vector<V2> p;
vector<LineSegment> s;
Boundary2D(vector<V2> po = {}, vector<LineSegment> seg = {});
};
Boundary2D::Boundary2D(vector<V2> po, vector<LineSegment> seg) {
p = po; s = seg;
}
class Interior2D {
private:
const long double threshold = 1e-7;
unsigned int i, j;
public:
vector<V2> p;
vector<Face> f;
Interior2D(vector<V2> po = {}, vector<Face> fa = {});
};
Interior2D::Interior2D(vector<V2> po, vector<Face> fa) {
p = po; f = fa;
}
class Domain2D {
public:
Boundary2D Boundary;
Interior2D Interior;
Domain2D(Boundary2D bnd = {}, Interior2D itr = {});
};
Domain2D::Domain2D(Boundary2D bnd, Interior2D itr) {
Boundary = bnd; Interior = itr;
}
int main()
{
return 0;
}
the error points to inside of the Domain2D constructor "Interior = itr" ("=" is marked red) and it says: function "Interior2D::operator=(const Interior2D &)" (declared implicitly) cannot be referenced -- it is a deleted function
and 'Interior2D &Interior2D::operator =(const Interior2D &)': attempting to reference a deleted function
. I have no clue what this is about, I thought it's just a standard constructor in case I would need to define a new Domain2D
like this: Domain2D d = Domain2D(some_bndry, some_interior)
instead of d.Boundary = ...
, d.Interior = ...
which is somewhat lengthy. What's the big deal with that Domain2D constructor, why does it raise that error?
P.S.: I found out that after commenting private:
section of Interior2D
class, the error vanishes. However there is the very same section in Boundary2D
class and that does not produce an error. Moreover, I need those variables (i
, j
, threshold
) to define some functions inside class (like adding a point, forming a segment or a triangle etc.). So it really boils down to: why having a private: i
, j
, threshold
variables in class causes the class constructor to raise the error?
P.S.2: I found out that making threshold
a regular long double variable (without const
), the error vanishes. If this is the only issue, the question is: why? The threshold
variable really acts like a constant that helps to tell if two doubles are "equal" or not and I certainly don't want it to change, it is a constant. Why aren't we allowed to make it constant? I've heard that it is better to make a variable constant if it remains constant. What does the constantness of a private variable have to do with some strange 'deleted function' error?