c – 错误:字段类型不完整

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.h和quaternion.h.包含警卫将确保每个标头只包含一次.其中一个将首先包括在内,另一个 – 第二个.在您的情况下,首先包含quaternion.h,这意味着Vec3在其中变为不完整的类型.这就是编译器告诉你的.

由于您尝试将Vec3对象用作Quaternion对象的直接成员,因此绝对需要Vec3为完整类型. quaternion.h标头必须包含vec3.h标头.该

class Vec3;

声明在quaternion.h中完全没有,所以你可以删除它.

鉴于上述情况,因此vec3.h不能包含quaternion.h,或者你最终会得到循环包含,它永远不会实现任何目标.从vec3.h中删除了quaternion.h的包含.保持

class Quaternion;

在vec3.h中声明并查看它是否以这种方式工作.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...