字段的类型不完整
Field has incomplete type?
所以我必须编写这个几何项目,其中有许多头文件。程序在编译时给了我一个错误: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
中,只使用了reference
到Line
,因此可以排除标头包含,只使用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&(,并消除循环依赖关系。
相关文章:
- 将结构字段的类型展开为可变模板参数
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 错误:字段'dateOfBirth'的类型不完整'Poco::Data::Date'
- 获取具有字段名称的 SELECT 字段类型,并带有 MariaDB C++连接器
- 无法初始化以 std::byte 作为成员类型的位字段
- 无法访问 Arduino 结构字段。错误"退出状态 1。xxxx 不命名类型"
- 在这种情况下,如何从类型 "const char*" 初始化字段?
- 确定输入"conscalc: calc"的类型时出错。不能联合编码器。具有不同字段集的结构类型
- 字段在正向声明中具有不完整的类型
- 使用固定大小类型时强制对位字段(C++14 之前)进行签名
- 返回对位字段的访问类型
- 将枚举类型保存在位字段 [dcl.enum] [class.bit] 中
- 将指向结构的指针转换为具有较少字段数的另一种结构类型
- 位字段成员的类型
- 无法汇总初始化类型结构的变量,其中包含在类模板中的位字段成员
- 在相同类型的字段上迭代
- Boost Xpressive-字段“ M_RX”具有不完整的类型
- 设计具有变体字段的文本类型类,其中可以存储一个或三个对象
- 自动复制=按值并支持多态性的适当类型的非静态字段
- 在std::set/std::map中存储具有多个不同类型字段的c++对象的有效方法