现场有不完整的类型?

时间:2014-06-08 14:34:12

标签: c++ class

所以我必须编写这个几何项目,其中我有许多头文件。该程序在编译时给了我这个错误: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。

2 个答案:

答案 0 :(得分:1)

您在 line.h 中包含 point.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

答案 1 :(得分:0)

计算Point构造函数中两条线的交点是一个糟糕的设计。在标题" Line.h"中有一个独立的功能点交叉点(const Line&,const Line&);并摆脱循环依赖。