C++ 错误:字段类型不完整

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3999400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:14:03  来源:igfitidea点击:

Error: Field has an incomplete type

c++incomplete-type

提问by Aero

quaternion.h:15: error: field ‘v' has incomplete type

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

Hi! I am stuck on an error that I cannot seem to solve.

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

Below is my code:

下面是我的代码:

#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

My Vec.h looks like this:

我的 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

Thanks for the help again =)

再次感谢您的帮助 =)

回答by AnT

Well, you have circular inclusion of two header files: vec3.hand quaternion.h. Include guards will make sure that each header is included only once. One of them will be included first, the other - second. In your case quaternion.his included first, meaning that Vec3becomes an incomplete type in it. This is what the compiler is telling you.

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

Since you are trying to use Vec3object as an immediate member of Quaternionobject, you absolutely need Vec3to be a complete type. The quaternion.hheader must include vec3.hheader. The

由于您试图将Vec3object 用作​​ object 的直接成员Quaternion,因此您绝对需要Vec3是一个完整的类型。该quaternion.h标题必须包含vec3.h头。这

class Vec3;

declaration achieves absolutely nothing in quaternion.h, so you can just remove it.

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

Given the above, it follows that vec3.hcannotinclude quaternion.h, or you'll end up with circular inclusion, which never achieves anything. Remove the inclusion of quaternion.hfrom vec3.h. Keep the

鉴于上述情况,因此vec3.h不能包含quaternion.h,否则您最终会得到循环包含,这永远不会实现任何目标。删除包含quaternion.hfrom vec3.h。保持

class Quaternion;

declaration in vec3.hand see if it works that way.

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

回答by Diego Sevilla

The problem is mutual inclusion of the .h. The compiler knows the types, but at some point they are incomplete. My advice would be to just forward declare Quarternionin the file vec3.h, but NOT include quaternion.h.

问题是.h. 编译器知道类型,但在某些时候它们是不完整的。我的建议是只Quarternion在文件中转发声明vec3.h,但不要包含quaternion.h.

Then, quaternion.hcan include vec3.hand everything will compile. Also, as JaredPar suggested, remove the forward declaration of Vec3in quaternion.h.

然后,quaternion.h可以包含vec3.h,一切都会编译。此外,正如 JaredPar 所建议的,删除Vec3in的前向声明quaternion.h

回答by flownt

the problem is that your files are include'ing eachother where there's no circular dependency. vec3 should not include quaternion then everything 'll be alright.

问题是您的文件在没有循环依赖的情况下包含了彼此。vec3 不应该包含四元数,然后一切都会好起来的。

now the error the compiler gave is because you predeclared vec3 but the full definition is not read if vec3 is included:

现在编译器给出的错误是因为您预先声明了 vec3 但如果包含 vec3 则不会读取完整定义:

vecr3.h --> include quaternion
quaternion.h --> include vec3 , but include-guards so nothing happens
quaternion.h --> predeclare Vec3,
quaternion.h --> try to use vec3 //fail
vec3.h --> actually declare vec3

vecr3.h --> 包含四
元数quaternion.h --> 包含 vec3 ,但包含保护,所以什么都不会发生
quaternion.h --> predeclare Vec3,
quaternion.h --> 尝试使用 vec3 //fail
vec3.h - -> 实际上声明 vec3

the only correct order is first vec3 include and only THEN the quaternion include.

唯一正确的顺序是第一个 vec3 包含,然后只有四元数包含。