c++ - 错误 : Field has an incomplete type

标签 c++ incomplete-type

quaternion.h:15: 错误:字段“v”的类型不完整

嗨!我陷入了一个我似乎无法解决的错误。

下面是我的代码:

#ifndef QUATERNION_H
#define QUATERNION_H

#include "vec3.h"

class Vec3;

class Quaternion
{

public:

 Quaternion(Vec3 v);

 Quaternion(double w, Vec3 v);

 Vec3 v; <--------------------------This is where the error is :(

 double scalar;



 Quaternion operator *(Quaternion s);

 Quaternion conjugate();

};



#endif

我的 Vec.h 看起来像这样:

#ifndef VEC3_H

#define VEC3_H



#include "point.h"

#include "quaternion.h"

#include <math.h>

class Quaternion;


class Vec3

{

 friend ofstream& operator <<(ofstream& output, const Vec3& p);

 friend ifstream& operator >>(ifstream& input, Vec3& p);



 public: 

 Vec3();

 Vec3(double _x, double _y);

 Vec3(double _x, double _y, double _z);



 double x,y,z;



 //Operators

 Vec3 operator -(Vec3 a) const;

 Vec3 operator /(double s) const;

 Vec3 operator *(double s) const;

 Vec3 operator *(Quaternion q) const;



 // Used to do vector Vec3 addition

 Vec3 operator +(Vec3 a) const;

 Point operator +(Point a) const;



 Vec3& operator =(Point a);



 Vec3 crossProduct(Vec3 v1); // Itself cross v1

 double dotProduct(Vec3 v);

 double length();

 void normalize();


};



#endif

再次感谢您的帮助 =)

最佳答案

好吧,您循环包含了两个头文件:vec3.hquaternion.h。包含守卫将确保每个 header 仅包含一次。其中一个将首先包括在内,另一个是第二个。在您的情况下, quaternion.h 首先包含在内,这意味着 Vec3 成为其中的不完整类型。这就是编译器告诉您的内容。

由于您试图将 Vec3 对象用作 Quaternion 对象的直接成员,因此您绝对需要 Vec3 是一个完整的类型。 quaternion.h header 必须包含 vec3.h header 。

class Vec3;

声明在 quaternion.h 中完全没有实现,因此您可以将其删除。

鉴于上述情况,vec3.h 不能包含 quaternion.h,否则您将以循环包含结束,这永远不会取得任何成就。从 vec3.h 中移除 quaternion.h。保持

class Quaternion;

vec3.h 中的声明,看看它是否这样工作。

关于c++ - 错误 : Field has an incomplete type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3999400/

相关文章:

c++ - STL 中有解引用迭代器吗?

c++ - STL 可迭代对象的迭代器循环宏

c++ - UDP 服务器发送回复失败,sendto 返回 -1,errno 为 22

c++ - 使用不完整类型的函数模板实例化

c++ - SFML 渲染目标.draw : Invalid use of incomplete type

c++ - void 和 C++ 中其他不完整类型的主要区别是什么?

C++ 系统文件 bits/stat.h 突然中断, "error: field ‘st_atim’ 类型不完整”

c++ - 如何在 Win32 上使用 C++ 安装硬件驱动程序?

c++ - 为什么只有定义了类才能将数据成员指定为类类型? (来自书 "C++ primer")

c++ - 如何避免在提升范围变换中复制