[]C++关于"模板类 运算符重载 类的前置声明"的一个混合有关问题
[求助]C++关于"模板类 运算符重载 类的前置声明"的一个混合问题
想实现一个3维点的类Point3D <T> 和3维向量的类Vector3D <T> , 在Point3D <T> 类中重载-实现两个两个点相减返回一个向量, 下面是我的代码:
-------------------------------------------------------
template <class T>
class Vector3D <T> ;
template <class T>
class Point3D
{
friend Vecotr3D <T> operator-(const Point3D <T> & p1, const Point3D <T> & p2)
{
Vector3D <T> v;
v.x = p1.x - p2.x;
v.y = p1.y - p2.y;
v.z = p1.z - p2.z;
return v;
}
...
...
public:
T x;
T y;
T z;
};
template <class T>
class Vector3D
{
...
...
public:
T x;
T y;
T z;
};
-------------------------------------------------------
出了很多错误。
如果函数operator-返回Point3D的话,就没有错误。
------解决方案--------------------
class Vector3D <T >;改成class Vector3D;vc6里编译通过
------解决方案--------------------
呵呵
我那个Vector3D要用到Point3D呀(一个构造函数), 所以还不能放在后面。
愁啊!
将代码的行号写出来:
---------------------------------
19: template <class T >
19: class Vector3D <T >;
20:
21: template <class T >
22: class Point3D
23: {
24: friend Vecotr3D <T > operator-(const Point3D <T >& p1, const Point3D <T >& p2)
25: {
26: Vector3D <T > v;
27:
28: v.x = p1.x - p2.x;
29: v.y = p1.y - p2.y;
30: v.z = p1.z - p2.z;
31:
32: return v;
33: }
============================
24: operator - 也用到了vector3D<T>的构造函数阿,所以这个设计本身具有循环依赖,forward declaration不可以用,事实上各个编译器对template的支持不尽相同,有的好像可以通过export关键字解决,但是可移植性很差。
我回这个帖子的意思是:C++重在设计
------解决方案--------------------
想实现一个3维点的类Point3D <T> 和3维向量的类Vector3D <T> , 在Point3D <T> 类中重载-实现两个两个点相减返回一个向量, 下面是我的代码:
-------------------------------------------------------
template <class T>
class Vector3D <T> ;
template <class T>
class Point3D
{
friend Vecotr3D <T> operator-(const Point3D <T> & p1, const Point3D <T> & p2)
{
Vector3D <T> v;
v.x = p1.x - p2.x;
v.y = p1.y - p2.y;
v.z = p1.z - p2.z;
return v;
}
...
...
public:
T x;
T y;
T z;
};
template <class T>
class Vector3D
{
...
...
public:
T x;
T y;
T z;
};
-------------------------------------------------------
出了很多错误。
如果函数operator-返回Point3D的话,就没有错误。
------解决方案--------------------
class Vector3D <T >;改成class Vector3D;vc6里编译通过
------解决方案--------------------
呵呵
我那个Vector3D要用到Point3D呀(一个构造函数), 所以还不能放在后面。
愁啊!
将代码的行号写出来:
---------------------------------
19: template <class T >
19: class Vector3D <T >;
20:
21: template <class T >
22: class Point3D
23: {
24: friend Vecotr3D <T > operator-(const Point3D <T >& p1, const Point3D <T >& p2)
25: {
26: Vector3D <T > v;
27:
28: v.x = p1.x - p2.x;
29: v.y = p1.y - p2.y;
30: v.z = p1.z - p2.z;
31:
32: return v;
33: }
============================
24: operator - 也用到了vector3D<T>的构造函数阿,所以这个设计本身具有循环依赖,forward declaration不可以用,事实上各个编译器对template的支持不尽相同,有的好像可以通过export关键字解决,但是可移植性很差。
我回这个帖子的意思是:C++重在设计
------解决方案--------------------
- C/C++ code
template<class T> class point3D { private: T x,y,z; public: point3D():x(),y(),z(){} point3D(const T&x_,const T&y_,const T&z_):x(x_),y(y_),z(z_){} point3D(const point3D<T>& copy) { x=copy.x; y=copy.y; z=copy.z; } const T getx() {return x;} const T gety() {return y;} const T getz() {return z;} const point3D<T> operator =(const point3D<T> ©) { x=copy.x; y=copy.y; z=copy.z; return *this; } void setx(const T& x_) {x=x_;} void sety(const T& y_) {y=y_;} void setz(const T& z_) {z=z_;} template<class U> friend class point3D; template<class U> point3D<T> operator -(point3D<U>& sr) { point3D<T> value; value.setx(x-T(sr.getx())); value.sety(y-T(sr.gety())); value.setz(y-T(sr.getz())); return value; } point3D<T> operator -(point3D<T>& sr) { point3D<T> value; value.setx(x-sr.getx()); value.sety(y-sr.gety()); value.setz(y-sr.getz()); return value; } };
------解决方案--------------------
- C/C++ code
template <class T > class Vector3D; template <class T> class Point3D { friend Vector3D<T> operator- (const Point3D <T>& p1, const Point3D <T>& p2) { Vector3D<T> v; v.x = p1.x - p2.x; v.y = p1.y - p2.y; v.z = p1.z - p2.z; return v; } public: T x; T y; T z; }; template <class T > class Vector3D { public: T x; T y; T z; };