我有一个班级Point
和Vector
Vector
继承自Point
,Point
使用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;
}
};
答案 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将在稍后声明。