微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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中声明并查看它是否以这种方式工作.

原文地址:https://www.jb51.cc/c/117316.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐