字段的类型不完整

Field has incomplete type?

本文关键字:类型 字段      更新时间:2023-10-16

所以我必须编写这个几何项目,其中有许多头文件。程序在编译时给了我一个错误:field 'p' has incomplete type。我认为问题在于这些文件是相互依赖的。这些是头文件:


#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
#include "Line.h"
class Point
{
  friend class Line;
  friend class Vector;
  double x;
  double y;
  double z;
 public:
   Point(double, double, double);
   Point ();
   Point (Line& , Line& );
   Point& operator=(Point&);
};

#endif // POINT_H_INCLUDED

#ifndef VECTOR_H_INCLUDED
#define VECTOR_H_INCLUDED
#include "Point.h"
class Point;
class Vector
{
  friend class Line;
  friend class Point;
  double a;
  double b;
  double c;
  public:
  Vector(double,double,double);
  Vector(Point&, Point&);
  Vector();
  Vector& operator=(Vector&);
};
#endif // VECTOR_H_INCLUDED

#ifndef LINE_H_INCLUDED
#define LINE_H_INCLUDED
#include "Point.h"
#include "Vector.h"
class Line
{
  Point p;
  Vector v;
  public:
  Line();
  Line(Point& , Vector&);
  Line(Point&,Point&);
};

#endif // LINE_H_INCLUDED

Point.h文件中给出了错误。我正在开发CodeBlocks。

您将点.h包含在行.h中,反之亦然,从而创建循环依赖。由于在类Point中,只使用了referenceLine,因此可以排除标头包含,只使用Line类的前向声明。

#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
//forward declare
class Line;
class Vector;
class Point
{
  friend class Line;
  friend class Vector;
  double x;
  double y;
  double z;
 public:
   Point(double, double, double);
   Point ();
   Point (Line& , Line& );
   Point& operator=(Point&);
};

#endif // POINT_H_INCLUDED

在point构造函数中计算两条线的交点是一个糟糕的设计。在标题"Line.h"中有一个独立的函数Point intersection(const Line&,const Line&(,并消除循环依赖关系。