Copy constructor with different struct in c++ -


how initialize ptype2 copy ptype3 struct (type-conversion)?

typedef struct ptype2 {     double x, y;      ptype2() : x(0), y(0) {}     ptype2(const ptype3 & ptype3) : x(ptype3.x), y(ptype3.y) {} //abort ptype3.z create two-dimensional point     ptype2(double xy) : x(xy), y(xy) {}     ptype2(double x, double y) : x(x), y(y) {} } ptype2; 

output:

error c2065: 'ptype3' : undeclared identifier error c2143: syntax error : missing ',' before '&' error c2228: left of '.a' must have class/struct/union error c2228: left of '.b' must have class/struct/union error c2664: 'ptype2::ptype2(const ptype2 &)' : cannot convert argument 1 'ptype3' 'const int' error c4430: missing type specifier - int assumed. note: c++ not support default-int 

i have 2 typedef struct, ptype2(double x, double y) , ptype3(double x, double y, double z).

an assignment operator overloading solve problem?

the compiler can't find ptype3. forward declaration won't suffice here, want access members of ptype3 in ptype2.

you can, however, implement constructors after defining ptype3, forward declaration trick.

struct ptype3; struct ptype2 {     double x, y;      ptype2() : x(0), y(0) {}     ptype2(const ptype3 & ptype3);     ptype2(double xy) : x(xy), y(xy) {}     ptype2(double x, double y) : x(x), y(y) {} };  struct ptype3 {     double x, y;      ptype3() : x(0), y(0) {}     ptype3(const ptype2 & ptype2) : x(ptype2.x), y(ptype2.y) {}     ptype3(double xy) : x(xy), y(xy) {}     ptype3(double x, double y) : x(x), y(y) {} }; 

and in cpp:

ptype2::ptype2(const ptype3 & ptype3) : x(ptype3.x), y.(ptype3.y) {}; 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -