声明类和继承时发生冲突

时间:2010-12-06 04:08:02

标签: c++

我有一个班级PointVector   Vector继承自PointPoint使用Vector作为朋友   我有一个cpp文件..我怎么解决这个冲突。   我不想使用头文件   我的代码是:

 class Vector:Point
 {
  //something
 };

 class Point
 {
 public:
     friend class Vector;

     double x;
     double y;
     Vector Point::operator-( Point Q)        // Vector diff of Points
     {
         Vector v;
         v.x = x - Q.x;
         v.y = y - Q.y;
         v.z = z - Q.z;
         v.dimn = max( dimn, Q.dim());
         return v;
     }

     Point Point::operator+( Vector v)        // +ve translation
     {
         Point P;
         P.x = x + v.x;
         P.y = y + v.y;
         P.z = z + v.z;
         P.dimn = max( dimn, v.dim());
         return P;
     }

     Point Point::operator-( Vector v)        // -ve translation
     {
         Point P;
         P.x = x - v.x;
         P.y = y - v.y;
         P.z = z - v.z;
         P.dimn = max( dimn, v.dim());
         return P;
     }

     Point& Point::operator+=( Vector v)        // +ve translation
     {
         x += v.x;
         y += v.y;
         z += v.z;
         dimn = max( dimn, v.dim());
         return *this;
     }

     Point& Point::operator-=( Vector v)        // -ve translation
     {
         x -= v.x;
         y -= v.y;
         z -= v.z;
         dimn = max( dimn, v.dim());
         return *this;
     }
};

3 个答案:

答案 0 :(得分:1)

尝试前瞻声明 -

class Vector;
class Point { ... }

答案 1 :(得分:1)

尝试像这样声明Point类:

class Point;

class Vector : Point
{ /* your code here */ };

class Point
{ /* your code here */ };

答案 2 :(得分:1)

在第一次使用点之前添加“前向声明”:

class Point;

这就是全部。它说有一个类Point将在稍后声明。