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

前端之家收集整理的这篇文章主要介绍了c – 错误:字段类型不完整前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
quaternion.h:15:错误:字段’v’的类型不完整

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

以下是我的代码

  1. #ifndef QUATERNION_H
  2. #define QUATERNION_H
  3.  
  4. #include "vec3.h"
  5.  
  6. class Vec3;
  7.  
  8. class Quaternion
  9. {
  10.  
  11. public:
  12.  
  13. Quaternion(Vec3 v);
  14.  
  15. Quaternion(double w,Vec3 v);
  16.  
  17. Vec3 v; <--------------------------This is where the error is :(
  18.  
  19. double scalar;
  20.  
  21.  
  22.  
  23. Quaternion operator *(Quaternion s);
  24.  
  25. Quaternion conjugate();
  26.  
  27. };
  28.  
  29.  
  30.  
  31. #endif

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

  1. #ifndef VEC3_H
  2.  
  3. #define VEC3_H
  4.  
  5.  
  6.  
  7. #include "point.h"
  8.  
  9. #include "quaternion.h"
  10.  
  11. #include <math.h>
  12.  
  13. class Quaternion;
  14.  
  15.  
  16. class Vec3
  17.  
  18. {
  19.  
  20. friend ofstream& operator <<(ofstream& output,const Vec3& p);
  21.  
  22. friend ifstream& operator >>(ifstream& input,Vec3& p);
  23.  
  24.  
  25.  
  26. public:
  27.  
  28. Vec3();
  29.  
  30. Vec3(double _x,double _y);
  31.  
  32. Vec3(double _x,double _y,double _z);
  33.  
  34.  
  35.  
  36. double x,y,z;
  37.  
  38.  
  39.  
  40. //Operators
  41.  
  42. Vec3 operator -(Vec3 a) const;
  43.  
  44. Vec3 operator /(double s) const;
  45.  
  46. Vec3 operator *(double s) const;
  47.  
  48. Vec3 operator *(Quaternion q) const;
  49.  
  50.  
  51.  
  52. // Used to do vector Vec3 addition
  53.  
  54. Vec3 operator +(Vec3 a) const;
  55.  
  56. Point operator +(Point a) const;
  57.  
  58.  
  59.  
  60. Vec3& operator =(Point a);
  61.  
  62.  
  63.  
  64. Vec3 crossProduct(Vec3 v1); // Itself cross v1
  65.  
  66. double dotProduct(Vec3 v);
  67.  
  68. double length();
  69.  
  70. void normalize();
  71.  
  72.  
  73. };
  74.  
  75.  
  76.  
  77. #endif

再次感谢您的帮助=)

解决方法

好吧,你有循环包含两个头文件:vec3.h和quaternion.h.包含警卫将确保每个标头只包含一次.其中一个将首先包括在内,另一个 – 第二个.在您的情况下,首先包含quaternion.h,这意味着Vec3在其中变为不完整的类型.这就是编译器告诉你的.

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

  1. class Vec3;

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

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

  1. class Quaternion;

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

猜你在找的C&C++相关文章