forward declaration

C

Chameleon

How can I do this:


struct Vector2D
{
Vector2D() : x(0), y(0) {}
explicit Vector2D(Vector3D &v) : x(v.x), y(v.y) {}
double x, y;
};

struct Vector3D
{
Vector3D() : x(0), y(0), z(0) {}
Vector3D(Vector2D &v) : x(v.x), y(v.y), z(0) {}
double x, y, z;
};
 
I

Ian Collins

How can I do this:


struct Vector2D
{
Vector2D() : x(0), y(0) {}
explicit Vector2D(Vector3D &v) : x(v.x), y(v.y) {}
double x, y;
};

struct Vector3D
{
Vector3D() : x(0), y(0), z(0) {}
Vector3D(Vector2D &v) : x(v.x), y(v.y), z(0) {}
double x, y, z;
};

Forward declare Vector3D and put the definition of Vector2D(Vector3D &v)
after the definition of Vector3D.
 
C

Chameleon

Στις 12/11/2010 00:12, ο/η Ian Collins έγÏαψε:
Forward declare Vector3D and put the definition of Vector2D(Vector3D &v)
after the definition of Vector3D.

I think, problem is more complex than that.

Class Vector2D has a c'tor with Vector3D param.
And Vector3D has a c'tor with Vector2D param.

Both of them, need access on other class members.
 
I

Ian Collins

Στις 12/11/2010 00:12, ο/η Ian Collins έγÏαψε:

I think, problem is more complex than that.

Class Vector2D has a c'tor with Vector3D param.
And Vector3D has a c'tor with Vector2D param.

Both of them, need access on other class members.

That's why I wrote "put the definition of Vector2D(Vector3D &v) after
the definition of Vector3D".
 
Z

ZMZ

Στις 12/11/2010 00:12, ο/η Ian Collins έγÏαψε:







I think, problem is more complex than that.

Class Vector2D has a c'tor with Vector3D param.
   And Vector3D has a c'tor with Vector2D param.

Both of them, need access on other class members.


Simply put, don't write inline constructor.

//"Vector2D.h"
class Vector3D;

class Vector2D
{
...
Vector2D(const Vector3D &);
....
};

//"Vector2D.cc"
#include "Vector2D.h"
#include "Vector3D.h"

Vector2D::Vector2D(const Vector3D& data) : (v0(data.v0)) ....
{
....
}
 
J

Juha Nieminen

ZMZ said:
Simply put, don't write inline constructor.

No, the problem is not the constructor being inline. The problem is the
constructor being defined in the class declaration. You can still make it
an inline function, just define it *after* the other class declaration.
Like:

class Vector3D;

class Vector2D
{
public:
Vector2D(const Vector3D&);
};

class Vector3D
{
public:
Vector3D(const Vector2D& v) { ... }
};

inline Vector2D::Vector2D(const Vector3D& v)
{
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top