C++ 声明类和继承中的冲突
我有一个类C++ 声明类和继承中的冲突,c++,C++,我有一个类点和向量 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
继承自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;
}
};
转发声明-
class Vector;
class Point { ... }
尝试按如下方式声明Point类:
class Point;
class Vector : Point
{ /* your code here */ };
class Point
{ /* your code here */ };
在第一次使用point之前添加“转发声明”:
class Point;
就这些。它说存在一个类点,稍后将声明。为什么不使用头文件?您希望代码的其余部分如何与这些类交互?当我fwd声明时,我得到了“struct Point”的前向声明为什么
Vector
继承自Point
(为什么它缺少访问说明符)?如果是孩子,为什么Point
afriend class
?我正在尝试不使用代码,看一看你可能会感觉到wgat,我正在尝试转发“struct Point”的声明@abbas作为猜测,您可能应该重新排列类以首先声明点,并将函数的实现移到主体之外和两个类之下。如果这样做没有帮助,您将不得不发布Vector中的一些代码以获得更多的答案。@AalokParikh:奇怪的是,这个答案包含与其他答案相同的信息。如果对简洁性有冒犯,请随意删除;)